From 3d864cd870209032d0aa58c39b40b9cf15c79d1e Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Fri, 10 Jan 2014 22:18:24 +0100 Subject: [PATCH] Remove B_PROT_* and related code Use standard error codes instead. This allows using error code returned by the underlying functions directly, and makes it possible to use strerror for debugging. So, we can also remove StatusString() from the various *Request classes. --- headers/os/net/HttpRequest.h | 8 -- headers/os/net/UrlRequest.h | 21 ---- src/kits/network/libnetapi/DataRequest.cpp | 10 +- src/kits/network/libnetapi/FileRequest.cpp | 113 ++++++++++-------- src/kits/network/libnetapi/HttpRequest.cpp | 43 ++----- src/kits/network/libnetapi/UrlRequest.cpp | 37 +----- .../libnetapi/UrlSynchronousRequest.cpp | 2 +- 7 files changed, 87 insertions(+), 147 deletions(-) diff --git a/headers/os/net/HttpRequest.h b/headers/os/net/HttpRequest.h index 4592302db1..cc35a85f76 100644 --- a/headers/os/net/HttpRequest.h +++ b/headers/os/net/HttpRequest.h @@ -49,7 +49,6 @@ public: status_t Stop(); const BUrlResult& Result() const; - const char* StatusString(status_t threadStatus) const; static bool IsInformationalStatusCode(int16 code); static bool IsSuccessStatusCode(int16 code); @@ -123,13 +122,6 @@ private: bool fOptAutoReferer : 1; }; -// ProtocolLoop return status -enum { - B_PROT_HTTP_NOT_FOUND = B_PROT_THREAD_STATUS__END, - B_PROT_HTTP_THREAD_STATUS__END -}; - - // Request method const char* const B_HTTP_GET = "GET"; const char* const B_HTTP_POST = "POST"; diff --git a/headers/os/net/UrlRequest.h b/headers/os/net/UrlRequest.h index 8444ed8434..641077f1f2 100644 --- a/headers/os/net/UrlRequest.h +++ b/headers/os/net/UrlRequest.h @@ -42,8 +42,6 @@ public: // URL protocol informations bool IsRunning() const; status_t Status() const; - virtual const char* StatusString(status_t threadStatus) - const; virtual const BUrlResult& Result() const = 0; @@ -66,23 +64,4 @@ protected: }; -// TODO: Rename, this is in the global namespace. -enum { - B_PROT_THREAD_STATUS__BASE = 0, - B_PROT_SUCCESS = B_PROT_THREAD_STATUS__BASE, - B_PROT_RUNNING, - B_PROT_PAUSED, - B_PROT_ABORTED, - B_PROT_SOCKET_ERROR, - B_PROT_CONNECTION_FAILED, - B_PROT_CANT_RESOLVE_HOSTNAME, - B_PROT_WRITE_FAILED, - B_PROT_READ_FAILED, - B_PROT_NO_MEMORY, - B_PROT_PROTOCOL_ERROR, - // Thread status over this one are guaranteed to be - // errors - B_PROT_THREAD_STATUS__END -}; - #endif // _B_URL_REQUEST_H_ diff --git a/src/kits/network/libnetapi/DataRequest.cpp b/src/kits/network/libnetapi/DataRequest.cpp index 1f1ce27cf7..8da5fcc95a 100644 --- a/src/kits/network/libnetapi/DataRequest.cpp +++ b/src/kits/network/libnetapi/DataRequest.cpp @@ -36,7 +36,7 @@ BDataRequest::_ProtocolLoop() BString mimeType; BString charset; const char* payload; - size_t length; + ssize_t length; bool isBase64 = false; fUrl.UrlDecode(true); @@ -86,6 +86,12 @@ BDataRequest::_ProtocolLoop() // payload must be a const char* so we can assign data.String() to // it below, but decode_64 modifies buffer. length = decode_base64(buffer, data.String(), data.Length()); + + // There may be some padding at the end of the base64 stream. This + // prevents us from computing the exact length we should get, so allow + // for some error margin. + if(length > data.Length() * 4 / 3 || length < data.Length() * 4 / 3 - 3) + return B_BAD_DATA; } else { payload = data.String(); length = data.Length(); @@ -102,5 +108,5 @@ BDataRequest::_ProtocolLoop() if (isBase64) delete[] payload; - return B_PROT_SUCCESS; + return B_OK; } diff --git a/src/kits/network/libnetapi/FileRequest.cpp b/src/kits/network/libnetapi/FileRequest.cpp index 2ebed5241c..69f8eebf29 100644 --- a/src/kits/network/libnetapi/FileRequest.cpp +++ b/src/kits/network/libnetapi/FileRequest.cpp @@ -7,6 +7,7 @@ */ +#include #include #include @@ -42,26 +43,35 @@ BFileRequest::_ProtocolLoop() { BNode node(fUrl.Path().String()); node_ref ref; - if (node.GetNodeRef(&ref) != B_OK) - return B_PROT_CONNECTION_FAILED; + status_t error = node.GetNodeRef(&ref); + if (error != B_OK) + return error; + ssize_t transferredSize = 0; if (node.IsFile()) { BFile file(fUrl.Path().String(), B_READ_ONLY); - if (file.InitCheck() != B_OK) - return B_PROT_CONNECTION_FAILED; + error = file.InitCheck(); + if (error != B_OK) + return error; // Send all notifications to listener, if any if (fListener != NULL) { fListener->ConnectionOpened(this); off_t size = 0; file.GetSize(&size); - fListener->DownloadProgress(this, size, size); fResult.SetLength(size); ssize_t chunkSize; char chunk[4096]; - while ((chunkSize = file.Read(chunk, sizeof(chunk))) > 0) + while ((chunkSize = file.Read(chunk, sizeof(chunk))) > 0) { fListener->DataReceived(this, chunk, chunkSize); + + if (chunkSize > 0) + transferredSize += chunkSize; + else if (transferredSize != size) + return chunkSize; + } + fListener->DownloadProgress(this, size, size); } BNodeInfo info(&file); @@ -69,52 +79,51 @@ BFileRequest::_ProtocolLoop() if (info.GetType(mimeType) == B_OK) fResult.SetContentType(mimeType); - return B_PROT_SUCCESS; - } else if (node.IsDirectory()) { - BDirectory directory(&ref); - - fResult.SetContentType("application/x-ftp-directory"); - // This tells WebKit to use its FTP directory rendering code. - - // Send all notifications to listener, if any - if (fListener != NULL) - fListener->ConnectionOpened(this); - - int size = 0; - char name[B_FILE_NAME_LENGTH]; - BEntry entry; - while (directory.GetNextEntry(&entry) != B_ENTRY_NOT_FOUND) { - // We read directories using the EPFL (Easily Parsed List Format) - // This happens to be one of the formats that WebKit can understand, - // and it is not too hard to parse or generate. - // http://tools.ietf.org/html/draft-bernstein-eplf-02 - BString epfl("+"); - if (entry.IsFile() || entry.IsSymLink()) { - epfl += "r,"; - off_t fileSize; - if (entry.GetSize(&fileSize) == B_OK) - epfl << "s" << fileSize << ","; - - } else if (entry.IsDirectory()) - epfl += "/,"; - - time_t modification; - if (entry.GetModificationTime(&modification) == B_OK) - epfl << "m" << modification << ","; - - entry.GetName(name); - epfl << "\t" << name << "\r\n"; - if (fListener != NULL) - fListener->DataReceived(this, epfl.String(), epfl.Length()); - size += epfl.Length(); - } - - if (fListener != NULL) - fListener->DownloadProgress(this, size, size); - fResult.SetLength(size); - - return B_PROT_SUCCESS; + return B_OK; } - return B_PROT_CONNECTION_FAILED; + assert(node.IsDirectory()); + BDirectory directory(&ref); + + fResult.SetContentType("application/x-ftp-directory"); + // This tells WebKit to use its FTP directory rendering code. + + // Send all notifications to listener, if any + if (fListener != NULL) + fListener->ConnectionOpened(this); + + int size = 0; + char name[B_FILE_NAME_LENGTH]; + BEntry entry; + while (directory.GetNextEntry(&entry) != B_ENTRY_NOT_FOUND) { + // We read directories using the EPFL (Easily Parsed List Format) + // This happens to be one of the formats that WebKit can understand, + // and it is not too hard to parse or generate. + // http://tools.ietf.org/html/draft-bernstein-eplf-02 + BString epfl("+"); + if (entry.IsFile() || entry.IsSymLink()) { + epfl += "r,"; + off_t fileSize; + if (entry.GetSize(&fileSize) == B_OK) + epfl << "s" << fileSize << ","; + + } else if (entry.IsDirectory()) + epfl += "/,"; + + time_t modification; + if (entry.GetModificationTime(&modification) == B_OK) + epfl << "m" << modification << ","; + + entry.GetName(name); + epfl << "\t" << name << "\r\n"; + if (fListener != NULL) + fListener->DataReceived(this, epfl.String(), epfl.Length()); + size += epfl.Length(); + } + + if (fListener != NULL) + fListener->DownloadProgress(this, size, size); + fResult.SetLength(size); + + return B_OK; } diff --git a/src/kits/network/libnetapi/HttpRequest.cpp b/src/kits/network/libnetapi/HttpRequest.cpp index 96f3bbcfb5..53a807e54f 100644 --- a/src/kits/network/libnetapi/HttpRequest.cpp +++ b/src/kits/network/libnetapi/HttpRequest.cpp @@ -24,12 +24,6 @@ static const int32 kHttpBufferSize = 4096; -static const char* kHttpProtocolThreadStrStatus[ - B_PROT_HTTP_THREAD_STATUS__END - B_PROT_THREAD_STATUS__END] - = { - "The remote server did not found the requested resource" - }; - BHttpRequest::BHttpRequest(const BUrl& url, bool ssl, const char* protocolName, BUrlProtocolListener* listener, BUrlContext* context) @@ -237,19 +231,6 @@ BHttpRequest::StatusCodeClass(int16 code) } -const char* -BHttpRequest::StatusString(status_t threadStatus) const -{ - if (threadStatus < B_PROT_THREAD_STATUS__END) - return BUrlRequest::StatusString(threadStatus); - else if (threadStatus >= B_PROT_HTTP_THREAD_STATUS__END) - return BUrlRequest::StatusString(-1); - else - return kHttpProtocolThreadStrStatus[threadStatus - - B_PROT_THREAD_STATUS__END]; -} - - const BUrlResult& BHttpRequest::Result() const { @@ -308,11 +289,11 @@ BHttpRequest::_ProtocolLoop() _EmitDebug(B_URL_PROTOCOL_DEBUG_ERROR, "Unable to resolve hostname (%s), aborting.", fUrl.Host().String()); - return B_PROT_CANT_RESOLVE_HOSTNAME; + return B_SERVER_NOT_FOUND; } status_t requestStatus = _MakeRequest(); - if (requestStatus != B_PROT_SUCCESS) + if (requestStatus != B_OK) return requestStatus; // Prepare the referer for the next request if needed @@ -396,9 +377,9 @@ BHttpRequest::_ProtocolLoop() fHeaders.CountHeaders(), fInputBuffer.Size()); if (fResult.StatusCode() == 404) - return B_PROT_HTTP_NOT_FOUND; + return B_RESOURCE_NOT_FOUND; - return B_PROT_SUCCESS; + return B_OK; } @@ -440,7 +421,7 @@ BHttpRequest::_MakeRequest() if (connectError != B_OK) { _EmitDebug(B_URL_PROTOCOL_DEBUG_ERROR, "Socket connection error %s", strerror(connectError)); - return B_PROT_CONNECTION_FAILED; + return connectError; } //! ProtocolHook:ConnectionOpened @@ -506,8 +487,8 @@ BHttpRequest::_MakeRequest() fSocket->Write("\r\n", 2); } - fSocket->Write(fOptPostFields->GetMultipartFooter().String(), - fOptPostFields->GetMultipartFooter().Length()); + BString footer = fOptPostFields->GetMultipartFooter(); + fSocket->Write(footer.String(), footer.Length()); } } else if ((fRequestMethod == B_HTTP_POST || fRequestMethod == B_HTTP_PUT) && fOptInputData != NULL) { @@ -546,7 +527,7 @@ BHttpRequest::_MakeRequest() bool receiveEnd = false; bool parseEnd = false; bool readByChunks = false; - bool readError = false; + status_t readError = B_OK; ssize_t bytesRead = 0; ssize_t bytesReceived = 0; ssize_t bytesTotal = 0; @@ -561,7 +542,7 @@ BHttpRequest::_MakeRequest() bytesRead = fSocket->Read(chunk.Data(), kHttpBufferSize); if (bytesRead < 0) { - readError = true; + readError = bytesRead; break; } else if (bytesRead == 0) receiveEnd = true; @@ -692,10 +673,10 @@ BHttpRequest::_MakeRequest() fSocket->Disconnect(); delete[] inputTempBuffer; - if (readError) - return B_PROT_READ_FAILED; + if (readError != B_OK) + return readError; - return fQuit ? B_PROT_ABORTED : B_PROT_SUCCESS; + return fQuit ? B_INTERRUPTED : B_OK; } diff --git a/src/kits/network/libnetapi/UrlRequest.cpp b/src/kits/network/libnetapi/UrlRequest.cpp index 351aa16686..b3168b113b 100644 --- a/src/kits/network/libnetapi/UrlRequest.cpp +++ b/src/kits/network/libnetapi/UrlRequest.cpp @@ -12,21 +12,6 @@ #include -static const char* kProtocolThreadStrStatus[B_PROT_THREAD_STATUS__END + 1] - = { - "Request successfully completed", - "Request running", - "Socket error", - "Connection failed", - "Hostname resolution failed", - "Network write failed", - "Network read failed", - "Out of memory", - "Protocol-specific error", - "Unknown error" - }; - - BUrlRequest::BUrlRequest(const BUrl& url, BUrlProtocolListener* listener, BUrlContext* context, const char* threadName, const char* protocolName) : @@ -35,7 +20,7 @@ BUrlRequest::BUrlRequest(const BUrl& url, BUrlProtocolListener* listener, fListener(listener), fQuit(false), fRunning(false), - fThreadStatus(B_PROT_PAUSED), + fThreadStatus(B_NO_INIT), fThreadId(0), fThreadName(threadName), fProtocol(protocolName) @@ -195,18 +180,6 @@ BUrlRequest::Status() const } -const char* -BUrlRequest::StatusString(status_t threadStatus) const -{ - if (threadStatus < B_PROT_THREAD_STATUS__BASE) - threadStatus = B_PROT_THREAD_STATUS__END; - else if (threadStatus >= B_PROT_PROTOCOL_ERROR) - threadStatus = B_PROT_PROTOCOL_ERROR; - - return kProtocolThreadStrStatus[threadStatus]; -} - - // #pragma mark Thread management @@ -214,7 +187,7 @@ BUrlRequest::StatusString(status_t threadStatus) const BUrlRequest::_ThreadEntry(void* arg) { BUrlRequest* urlProtocol = reinterpret_cast(arg); - urlProtocol->fThreadStatus = B_PROT_RUNNING; + urlProtocol->fThreadStatus = B_BUSY; status_t protocolLoopExitStatus = urlProtocol->_ProtocolLoop(); @@ -223,7 +196,7 @@ BUrlRequest::_ThreadEntry(void* arg) if (urlProtocol->fListener != NULL) { urlProtocol->fListener->RequestCompleted(urlProtocol, - protocolLoopExitStatus == B_PROT_SUCCESS); + protocolLoopExitStatus == B_OK); } return B_OK; @@ -240,8 +213,8 @@ BUrlRequest::_EmitDebug(BUrlProtocolDebugMessage type, va_list arguments; va_start(arguments, format); - char debugMsg[256]; - vsnprintf(debugMsg, 256, format, arguments); + char debugMsg[1024]; + vsnprintf(debugMsg, sizeof(debugMsg), format, arguments); fListener->DebugMessage(this, type, debugMsg); va_end(arguments); } diff --git a/src/kits/network/libnetapi/UrlSynchronousRequest.cpp b/src/kits/network/libnetapi/UrlSynchronousRequest.cpp index c58479e47d..050d46b339 100644 --- a/src/kits/network/libnetapi/UrlSynchronousRequest.cpp +++ b/src/kits/network/libnetapi/UrlSynchronousRequest.cpp @@ -107,6 +107,6 @@ void BUrlSynchronousRequest::RequestCompleted(BUrlRequest* caller, bool success) { PRINT(("SynchronousRequest::RequestCompleted(%s) : %s\n", (success?"true":"false"), - caller->StatusString(caller->Status()))); + strerror(caller->Status()))); fRequestComplete = true; }