Tweak HTTP handling for POST requests.

Tolerate slightly malformed request URLs (no slash following the host specified), and use a better check when POST-ing to decide the request-body data source.
This commit is contained in:
James Turner
2012-11-17 18:08:38 +00:00
parent 9f31addfa5
commit d1af42e9ad
2 changed files with 10 additions and 7 deletions
+3 -3
View File
@@ -172,6 +172,7 @@ public:
stringstream headerData; stringstream headerData;
string path = r->path(); string path = r->path();
assert(!path.empty());
string query = r->query(); string query = r->query();
string bodyData; string bodyData;
@@ -179,7 +180,7 @@ public:
path = r->scheme() + "://" + r->host() + r->path(); path = r->scheme() + "://" + r->host() + r->path();
} }
if (r->method() == "POST") { if (r->requestBodyType() == CONTENT_TYPE_URL_ENCODED) {
headerData << r->method() << " " << path << " HTTP/1.1\r\n"; headerData << r->method() << " " << path << " HTTP/1.1\r\n";
bodyData = query.substr(1); // URL-encode, drop the leading '?' bodyData = query.substr(1); // URL-encode, drop the leading '?'
headerData << "Content-Type:" << CONTENT_TYPE_URL_ENCODED << "\r\n"; headerData << "Content-Type:" << CONTENT_TYPE_URL_ENCODED << "\r\n";
@@ -429,7 +430,7 @@ public:
private: private:
bool connectToHost() bool connectToHost()
{ {
SG_LOG(SG_IO, SG_INFO, "HTTP connecting to " << host << ":" << port); SG_LOG(SG_IO, SG_DEBUG, "HTTP connecting to " << host << ":" << port);
if (!open()) { if (!open()) {
SG_LOG(SG_ALL, SG_WARN, "HTTP::Connection: connectToHost: open() failed"); SG_LOG(SG_ALL, SG_WARN, "HTTP::Connection: connectToHost: open() failed");
@@ -657,7 +658,6 @@ void Client::update(int waitTimeout)
_connections.erase(del); _connections.erase(del);
} else { } else {
if (it->second->shouldStartNext()) { if (it->second->shouldStartNext()) {
SG_LOG(SG_IO, SG_INFO, "should start next, hmm");
it->second->tryStartNextRequest(); it->second->tryStartNextRequest();
} }
+4 -1
View File
@@ -113,7 +113,9 @@ string Request::path() const
int hostEnd = u.find('/', schemeEnd + 3); int hostEnd = u.find('/', schemeEnd + 3);
if (hostEnd < 0) { if (hostEnd < 0) {
return ""; // couldn't parse host // couldn't parse host, or URL looks like 'http://foo.com' (no trailing '/')
// fixup to root resource path: '/'
return "/";
} }
int query = u.find('?', hostEnd + 1); int query = u.find('?', hostEnd + 1);
@@ -203,6 +205,7 @@ void Request::setFailure(int code, const std::string& reason)
void Request::failed() void Request::failed()
{ {
// no-op in base class // no-op in base class
SG_LOG(SG_IO, SG_INFO, "request failed:" << url());
} }
Request::HTTPVersion Request::decodeVersion(const string& v) Request::HTTPVersion Request::decodeVersion(const string& v)