Commit dccd2a2373d9050d3c847355cf516ec33b47ff5a

Authored by Matti Virkkunen
1 parent a1590b5a

Calculate correct response body length in HTTPClient.

libpurple/httpclient.cpp
... ... @@ -99,26 +99,6 @@ void HTTPClient::execute_next() {
99 99 }
100 100 }
101 101  
102   -void HTTPClient::parse_response(const char *res, int &status, const guchar *&body) {
103   - // libpurple guarantees that responses are null terminated even if they're binary, so string
104   - // functions are safe to use.
105   -
106   - const char *status_end = strstr(res, "\r\n");
107   - if (!status_end)
108   - return;
109   -
110   - const char *header_end = strstr(res, "\r\n\r\n");
111   - if (!header_end)
112   - return;
113   -
114   - std::stringstream ss(std::string(res, status_end - res));
115   - ss.ignore(255, ' ');
116   -
117   - ss >> status;
118   -
119   - body = (const guchar *)(header_end + 4);
120   -}
121   -
122 102 void HTTPClient::complete(HTTPClient::Request *req,
123 103 const gchar *url_text, gsize len, const gchar *error_message)
124 104 {
... ... @@ -126,14 +106,27 @@ void HTTPClient::complete(HTTPClient::Request *req,
126 106 purple_debug_error("util", "HTTP error: %s\n", error_message);
127 107 req->callback(-1, nullptr, 0);
128 108 } else {
129   - // HTTP/1.1 200 OK
130   -
131   - int status;
  109 + int status = 0;
132 110 const guchar *body = nullptr;
  111 + gsize body_len = 0;
  112 +
  113 + // libpurple guarantees that responses are null terminated even if they're binary, so
  114 + // string functions are safe to use.
  115 +
  116 + const char *status_end = strstr(url_text, "\r\n"),
  117 + *header_end = strstr(url_text, "\r\n\r\n");
133 118  
134   - parse_response(url_text, status, body);
  119 + if (status_end && header_end) {
  120 + std::stringstream ss(std::string(url_text, status_end - url_text));
  121 + ss.ignore(255, ' ');
  122 +
  123 + ss >> status;
  124 +
  125 + body = (const guchar *)(header_end + 4);
  126 + body_len = len - (header_end - url_text + 4);
  127 + }
135 128  
136   - req->callback(status, body, len);
  129 + req->callback(status, body, body_len);
137 130 }
138 131  
139 132 request_queue.remove(req);
... ...
libpurple/httpclient.hpp
... ... @@ -42,7 +42,6 @@ class HTTPClient {
42 42 int in_flight;
43 43  
44 44 void execute_next();
45   - void parse_response(const char *res, int &status, const guchar *&body);
46 45 void complete(Request *req, const gchar *url_text, gsize len, const gchar *error_message);
47 46  
48 47 static void purple_cb(PurpleUtilFetchUrlData *url_data, gpointer user_data,
... ...