int TickDbImpl::executeRequest()

in src/dxapi/native/tickdb/http/tickdb_http.cpp [1120:1179]


int TickDbImpl::executeRequest( string& responseBody,
                                const char * request,
                                const char * path,
                                const std::string * requestBody,
                                const char * requestName,
                                bool allowUnopenedDb)
{
    byte buffer[0x2000 + 0x200];
    uintptr nReadTotal = 0;
    intptr contentLength;
    HTTP::ResponseHeader responseHeader;
    unsigned httpResponseCode = 0;

    // No 'isOpen()' check for some requests. 'Format' should be called on closed database 
    requestName = NULL == requestName ? "" : requestName;

    if (!allowUnopenedDb && !isOpen()) {
        DBGLOG(LOGHDR ".executeRequest(%s): TickDb is not in 'open' state", ID, requestName);
        return 0;
    }

    unique_ptr<IOStream> ioStream(sendRequest(request, path, requestBody, HTTP::ContentType::XML, HTTP::ContentLength::FIXED, TCP::NO_DELAY, false));
    DataReaderMsg reader(buffer + 0x100, 0x2000, 0, ioStream.get());

    // This is additional debug logging code for the case http://rm.orientsoft.by/issues/4287 (Garbage response header returned sometimes)
    try {
        httpResponseCode = HTTP::readResponseHeader(responseHeader, reader);
    }
    catch (...) {
        DBGLOG(LOGHDR, ".executeRequest(): ERR: Unable to parse HTTP response header (bytes written: %llu, read: %llu, resp.hdr.size: %llu):\n",
            ID, (ulonglong)ioStream->nBytesWritten(), (ulonglong)ioStream->nBytesRead(), (ulonglong)responseHeader.size(), responseHeader.c_str());
        throw;
    }

    // Not successful?
    contentLength = responseHeader.parseContentLength();
    if (!HttpSuccess(httpResponseCode)) {
        switch (httpResponseCode) {
        case 401:    
            DBGLOG(LOGHDR ".executeRequest(%s): 401 Unauthorized", ID, requestName);
            break;
        }

        if (HTTP::readResponseBody(responseBody, reader, contentLength) && responseBody.length()) {
            HTTP::tryExtractTomcatResponseMessage(responseBody);
        } else {
            // If not able to read response body, copy http error text there
            responseBody = responseHeader.responseText();
        }

        return httpResponseCode;
    }
	
	// todo: close output causes exception due executing response.
    //ioStream->closeOutput();
    bool result = HTTP::readResponseBody(responseBody, reader, contentLength);
    ioStream->close();

    return result ? httpResponseCode : 0;
}