replaced stringstream with sprintf in http_connection

This commit is contained in:
Arvid Norberg 2009-04-12 09:00:16 +00:00
parent 6a11d6ee4e
commit 58b053bfdf
1 changed files with 20 additions and 15 deletions

View File

@ -83,43 +83,48 @@ void http_connection::get(std::string const& url, time_duration timeout, int pri
} }
#endif #endif
std::stringstream headers; char request[2048];
char* end = request + sizeof(request);
char* ptr = request;
#define APPEND_FMT(fmt) ptr += snprintf(ptr, end - ptr, fmt)
#define APPEND_FMT1(fmt, arg) ptr += snprintf(ptr, end - ptr, fmt, arg)
#define APPEND_FMT2(fmt, arg1, arg2) ptr += snprintf(ptr, end - ptr, fmt, arg1, arg2)
if (ps && (ps->type == proxy_settings::http if (ps && (ps->type == proxy_settings::http
|| ps->type == proxy_settings::http_pw) || ps->type == proxy_settings::http_pw)
&& !ssl) && !ssl)
{ {
// if we're using an http proxy and not an ssl // if we're using an http proxy and not an ssl
// connection, just do a regular http proxy request // connection, just do a regular http proxy request
headers << "GET " << url << " HTTP/1.0\r\n"; APPEND_FMT1("GET %s HTTP/1.0\r\n", url.c_str());
if (ps->type == proxy_settings::http_pw) if (ps->type == proxy_settings::http_pw)
headers << "Proxy-Authorization: Basic " << base64encode( APPEND_FMT1("Proxy-Authorization: Basic %s\r\n", base64encode(
ps->username + ":" + ps->password) << "\r\n"; ps->username + ":" + ps->password).c_str());
hostname = ps->hostname; hostname = ps->hostname;
port = ps->port; port = ps->port;
ps = 0; ps = 0;
} }
else else
{ {
headers << "GET " << path << " HTTP/1.0\r\n" APPEND_FMT2("GET %s HTTP/1.0\r\n"
"Host: " << hostname; "Host: %s", path.c_str(), hostname.c_str());
if (port != default_port) headers << ":" << to_string(port).elems; if (port != default_port) APPEND_FMT1(":%d\r\n", port);
headers << "\r\n"; else APPEND_FMT("\r\n");
} }
if (!auth.empty()) if (!auth.empty())
headers << "Authorization: Basic " << base64encode(auth) << "\r\n"; APPEND_FMT1("Authorization: Basic %s\r\n", base64encode(auth).c_str());
if (!user_agent.empty()) if (!user_agent.empty())
headers << "User-Agent: " << user_agent << "\r\n"; APPEND_FMT1("User-Agent: %s\r\n", user_agent.c_str());
if (m_bottled) if (m_bottled)
headers << "Accept-Encoding: gzip\r\n"; APPEND_FMT("Accept-Encoding: gzip\r\n");
headers << APPEND_FMT("Connection: close\r\n\r\n");
"Connection: close\r\n"
"\r\n";
sendbuffer = headers.str(); sendbuffer.assign(request);
m_url = url; m_url = url;
start(hostname, to_string(port).elems, timeout, prio start(hostname, to_string(port).elems, timeout, prio
, ps, ssl, handle_redirects, bind_addr); , ps, ssl, handle_redirects, bind_addr);