1 /* 2 * Copyright 2016, Dario Casalinuovo 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include "HTTPMediaIO.h" 8 9 10 HTTPMediaIO::HTTPMediaIO(BUrl* url) 11 : 12 fContext(), 13 fBuffer(), 14 fInitErr(B_ERROR) 15 { 16 fContext->AcquireReference(); 17 18 fReq = new BHttpRequest(*url); 19 fReq->SetContext(fContext); 20 fReq->Run(); 21 fReq->AdoptInputData(fBuffer); 22 23 if (!fReq->IsRunning()) 24 return; 25 26 fInitErr = _IntegrityCheck(); 27 } 28 29 30 HTTPMediaIO::~HTTPMediaIO() 31 { 32 fContext->ReleaseReference(); 33 delete fContext; 34 delete fReq; 35 } 36 37 38 status_t 39 HTTPMediaIO::InitCheck() const 40 { 41 return fInitErr; 42 } 43 44 45 ssize_t 46 HTTPMediaIO::ReadAt(off_t position, void* buffer, size_t size) 47 { 48 return fBuffer->ReadAt(position, buffer, size); 49 } 50 51 52 ssize_t 53 HTTPMediaIO::WriteAt(off_t position, const void* buffer, size_t size) 54 { 55 return B_NOT_SUPPORTED; 56 } 57 58 59 off_t 60 HTTPMediaIO::Seek(off_t position, uint32 seekMode) 61 { 62 return fBuffer->Seek(position, seekMode); 63 } 64 65 off_t 66 HTTPMediaIO::Position() const 67 { 68 return fBuffer->Position(); 69 } 70 71 72 status_t 73 HTTPMediaIO::SetSize(off_t size) 74 { 75 return B_NOT_SUPPORTED; 76 } 77 78 79 status_t 80 HTTPMediaIO::GetSize(off_t* size) const 81 { 82 return B_NOT_SUPPORTED; 83 } 84 85 86 bool 87 HTTPMediaIO::IsSeekable() const 88 { 89 return false; 90 } 91 92 93 bool 94 HTTPMediaIO::IsEndless() const 95 { 96 return true; 97 } 98 99 100 status_t 101 HTTPMediaIO::_IntegrityCheck() 102 { 103 const BHttpResult& r = dynamic_cast<const BHttpResult&>(fReq->Result()); 104 if (r.StatusCode() != 200) 105 return B_ERROR; 106 107 if (BString("OK")!= r.StatusText()) 108 return B_ERROR; 109 110 return B_OK; 111 } 112