Subversion Repositories tpanel

Rev

Rev 94 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 94 Rev 97
Line 93... Line 93...
93
        return nullptr;
93
        return nullptr;
94
    }
94
    }
95
 
95
 
96
    string request = makeRequest(URL);
96
    string request = makeRequest(URL);
97
 
97
 
98
    if (TError::isError())
98
    if (TError::isError() || request.empty())
99
    {
99
    {
100
        delete[] buffer;
100
        delete[] buffer;
101
        return nullptr;
101
        return nullptr;
102
    }
102
    }
103
 
103
 
Line 180... Line 180...
180
    }
180
    }
181
 
181
 
182
    char buf[8194];
182
    char buf[8194];
183
    memset(buf, 0, sizeof(buf));
183
    memset(buf, 0, sizeof(buf));
184
    size_t pos = 0, length = 0;
184
    size_t pos = 0, length = 0;
185
    size_t rlen;
185
    size_t rlen, totalLen = 0;
-
 
186
    int loop = 0;
186
 
187
 
187
    try
188
    try
188
    {
189
    {
-
 
190
        std::chrono::steady_clock::time_point timePoint = std::chrono::steady_clock::now();
-
 
191
        totalLen = 0;
-
 
192
 
189
        while ((rlen = receive(buf, sizeof(buf))) > 0 && rlen != TSocket::npos)
193
        while ((rlen = receive(buf, sizeof(buf))) > 0 && rlen != TSocket::npos)
190
        {
194
        {
191
            size_t len = rlen;
195
            size_t len = rlen;
192
 
196
 
-
 
197
            if (totalLen == 0 && loop < 2)
-
 
198
            {
-
 
199
                // Let's see if we have already the content length
-
 
200
                char *cLenPos = nullptr;
-
 
201
 
-
 
202
                if ((cLenPos = strnstr(buf, "Content-Length:", rlen)) != nullptr)
-
 
203
                {
-
 
204
                    cLenPos += 16;  // Start of content length information
-
 
205
                    size_t cLen = atol(cLenPos);
-
 
206
 
-
 
207
                    char *cStart = strstr(buf, "\r\n\r\n"); // Find start of content
-
 
208
 
-
 
209
                    if (cStart)
-
 
210
                        totalLen = cLen + ((cStart + 4) - buf);
-
 
211
 
-
 
212
                    MSG_DEBUG("Total length: " << totalLen << ", content length: " << cLen);
-
 
213
                }
-
 
214
            }
-
 
215
 
193
            if ((pos + len) >= bufsize)
216
            if ((pos + len) >= bufsize)
194
            {
217
            {
195
                renew(&buffer, bufsize, bufsize + MAX_BLOCK);
218
                renew(&buffer, bufsize, bufsize + MAX_BLOCK);
196
 
219
 
197
                if (!buffer)
220
                if (!buffer)
Line 208... Line 231...
208
                memcpy(buffer+pos, buf, len);
231
                memcpy(buffer+pos, buf, len);
209
                pos += len;
232
                pos += len;
210
                length += len;
233
                length += len;
211
            }
234
            }
212
 
235
 
-
 
236
            if (length && totalLen && length >= totalLen)
-
 
237
                break;
-
 
238
 
213
            memset(buf, 0, sizeof(buf));
239
            memset(buf, 0, sizeof(buf));
-
 
240
            loop++;
-
 
241
        }
-
 
242
 
-
 
243
        if (TStreamError::checkFilter(HLOG_DEBUG))
-
 
244
        {
-
 
245
            std::chrono::steady_clock::time_point endPoint = std::chrono::steady_clock::now();
-
 
246
            std::chrono::nanoseconds difftime = endPoint - timePoint;
-
 
247
            std::chrono::seconds secs = std::chrono::duration_cast<std::chrono::seconds>(difftime);
-
 
248
            std::chrono::milliseconds msecs = std::chrono::duration_cast<std::chrono::milliseconds>(difftime) - std::chrono::duration_cast<std::chrono::seconds>(secs);
-
 
249
            std::stringstream s;
-
 
250
            s << std::chrono::duration_cast<std::chrono::nanoseconds> (difftime).count() << "[ns]" << " --> " << std::chrono::duration_cast<std::chrono::seconds>(secs).count() << "s " << std::chrono::duration_cast<std::chrono::milliseconds>(msecs).count() << "ms";
-
 
251
            MSG_DEBUG("[" << mURL.host << "] Elapsed time for receive: " << s.str());
214
        }
252
        }
215
    }
253
    }
216
    catch(TXceptNetwork& e)
254
    catch(TXceptNetwork& e)
217
    {
255
    {
218
        MSG_ERROR("Error reading from " << mURL.host << ": " << e.what());
256
        MSG_ERROR("Error reading from " << mURL.host << ": " << e.what());
Line 618... Line 656...
618
string THTTPClient::makeRequest(const string& url)
656
string THTTPClient::makeRequest(const string& url)
619
{
657
{
620
    DECL_TRACER("THTTPClient::makeRequest(const string& url)");
658
    DECL_TRACER("THTTPClient::makeRequest(const string& url)");
621
 
659
 
622
    URL_t uparts = parseURL(url);
660
    URL_t uparts = parseURL(url);
-
 
661
 
-
 
662
    if (uparts.host == "0.0.0.0" || uparts.host == "8.8.8.8")
-
 
663
    {
-
 
664
        MSG_WARNING("Refusing to connect to host " << uparts.host << "!");
-
 
665
        return string();
-
 
666
    }
-
 
667
 
623
    string request = "GET " + uparts.path + " HTTP/1.1\r\n";
668
    string request = "GET " + uparts.path + " HTTP/1.1\r\n";
624
    request += "Host: " + uparts.host;
669
    request += "Host: " + uparts.host;
625
 
670
 
626
    if (uparts.port > 0 && uparts.port != 80 && uparts.port != 443)
671
    if (uparts.port > 0 && uparts.port != 80 && uparts.port != 443)
627
        request += ":" + std::to_string(uparts.port);
672
        request += ":" + std::to_string(uparts.port);