diff options
Diffstat (limited to 'fbench/src/httpclient/httpclient.cpp')
-rw-r--r-- | fbench/src/httpclient/httpclient.cpp | 47 |
1 files changed, 13 insertions, 34 deletions
diff --git a/fbench/src/httpclient/httpclient.cpp b/fbench/src/httpclient/httpclient.cpp index 9265905c9f6..2feed087b43 100644 --- a/fbench/src/httpclient/httpclient.cpp +++ b/fbench/src/httpclient/httpclient.cpp @@ -110,8 +110,7 @@ HTTPClient::ReadLine(char *buf, size_t bufsize) bool HTTPClient::Connect(const char *url, bool usePost, const char *content, int cLen) { - char tmp[4096]; - char *req = NULL; + std::unique_ptr<char[]> req; uint32_t req_max = 0; uint32_t url_len = strlen(url); uint32_t host_len = _hostname.size(); @@ -126,14 +125,8 @@ HTTPClient::Connect(const char *url, bool usePost, const char *content, int cLen headers += "X-Yahoo-Vespa-Benchmarkdata-Coverage: true\r\n"; } - if (url_len + host_len + headers.length() + FIXED_REQ_MAX < sizeof(tmp)) { - req = tmp; - req_max = sizeof(tmp); - } else { - req_max = url_len + host_len + headers.length() + FIXED_REQ_MAX; - req = new char[req_max]; - assert(req != NULL); - } + req_max = url_len + host_len + headers.length() + FIXED_REQ_MAX; + req = std::make_unique<char []>(req_max); if (!_keepAlive) { headers += "Connection: close\r\n"; @@ -142,7 +135,7 @@ HTTPClient::Connect(const char *url, bool usePost, const char *content, int cLen // create request if (usePost) { - snprintf(req, req_max, + snprintf(req.get(), req_max, "POST %s HTTP/1.1\r\n" "Host: %s\r\n" "Content-Length: %d\r\n" @@ -150,7 +143,7 @@ HTTPClient::Connect(const char *url, bool usePost, const char *content, int cLen "\r\n", url, _host_header_value.c_str(), cLen, headers.c_str()); } else { - snprintf(req, req_max, + snprintf(req.get(), req_max, "GET %s HTTP/1.1\r\n" "Host: %s\r\n" "%s" @@ -159,18 +152,16 @@ HTTPClient::Connect(const char *url, bool usePost, const char *content, int cLen } // try to reuse connection if keep-alive is enabled + ssize_t reqLen = strlen(req.get()); if (_keepAlive && _socket - && _socket->write(req, strlen(req)) == (ssize_t)strlen(req) + && _socket->write(req.get(), reqLen) == reqLen && (!usePost || _socket->write(content, cLen) == (ssize_t)cLen) && FillBuffer() > 0) { // DEBUG // printf("Socket Connection reused!\n"); _reuseCount++; - if (req != tmp) { - delete [] req; - } return true; } else { _socket.reset(); @@ -179,25 +170,14 @@ HTTPClient::Connect(const char *url, bool usePost, const char *content, int cLen // try to open new connection to server if (connect_socket() - && _socket->write(req, strlen(req)) == (ssize_t)strlen(req) + && _socket->write(req.get(), reqLen) == reqLen && (!usePost || _socket->write(content, cLen) == (ssize_t)cLen)) { - - // DEBUG - // printf("New Socket connection!\n"); - if (req != tmp) { - delete [] req; - } return true; } else { _socket.reset(); } - // DEBUG - // printf("Connect FAILED!\n"); - if (req != tmp) { - delete [] req; - } return false; } @@ -214,7 +194,7 @@ HTTPClient::SplitString(char *input, int &argc, char **argv, int maxargs) } if (*(argv[argc]) != '\0') argc++; - return NULL; // COMPLETE + return nullptr; // COMPLETE } bool @@ -241,8 +221,7 @@ HTTPClient::ReadHTTPHeader(std::string & headerinfo) if (argc >= 2) { if (strncmp(argv[0], "HTTP/", 5) != 0) return false; - _httpVersion = (strncmp(argv[0], "HTTP/1.0", 8) == 0) ? - 0 : 1; + _httpVersion = (strncmp(argv[0], "HTTP/1.0", 8) == 0) ? 0 : 1; _requestStatus = atoi(argv[1]); } else { return false; @@ -319,7 +298,7 @@ HTTPClient::ReadChunkHeader() char c; int i; - if (_chunkSeq++ > 0 && ReadLine(NULL, 0) != 0) + if (_chunkSeq++ > 0 && ReadLine(nullptr, 0) != 0) return false; // no CRLF(/LF) after data block assert(_chunkLeft == 0); @@ -344,7 +323,7 @@ HTTPClient::ReadChunkHeader() // printf("CHUNK: Length: %d\n", _chunkLeft); if (_chunkLeft == 0) { - while ((lineLen = ReadLine(NULL, 0)) > 0); // skip trailer + while ((lineLen = ReadLine(nullptr, 0)) > 0); // skip trailer if (lineLen < 0) return false; // data error _dataDone = true; // got last chunk @@ -548,7 +527,7 @@ HTTPClient::Fetch(const char *url, std::ostream *file, } while((readRes = Read(buf, buflen)) > 0) { - if(file != NULL) { + if(file != nullptr) { if (!file->write(buf, readRes)) { Close(); return FetchStatus(false, _requestStatus, _totalHitCount, written); |