HTTPAPI_RESULT HTTPAPI_SetOption(HTTP_HANDLE handle, const char* optionName, const void* value) { HTTPSClient* client = (HTTPSClient*)handle; HTTPAPI_RESULT result = HTTPAPI_INVALID_ARG; if (strcmp("timeout", optionName) == 0) { client->setTimeout(*(const unsigned int*)value); result = HTTPAPI_OK; } return result; }
void FetchClient::connect_ready() { #ifdef USE_OPENSSL if (m_state == None){ m_socket->setRaw(true); m_socket->readBuffer.init(0); HTTPSClient *https = new HTTPSClient(m_socket->socket()); if (!https->init()){ m_socket->error_state("Can't initialize HTTPS"); return; } m_state = SSLConnect; m_socket->setSocket(https); https->connect(); https->process(); return; } #endif log(L_DEBUG, "HTTP connect ready"); m_socket->setRaw(true); m_socket->writeBuffer.packetStart(); m_socket->writeBuffer << (m_post ? "POST " : "GET ") << m_uri.c_str() << " HTTP/1.0\r\n"; for (HEADERS_MAP::iterator it = m_hOut.begin(); it != m_hOut.end(); ++it){ m_socket->writeBuffer << (*it).first.c_str() << ": " << (*it).second.c_str() << "\r\n"; } m_socket->writeBuffer << "\r\n"; if (m_post) m_socket->writeBuffer.pack(m_post->data(), m_post->size()); log_packet(m_socket->writeBuffer, true, HTTPPacket); m_socket->write(); m_socket->readBuffer.init(0); m_socket->readBuffer.packetStart(); }
void FetchClient::connect_ready() { #ifdef USE_OPENSSL if ((m_state == None) & m_bHTTPS){ m_socket->setRaw(true); m_socket->readBuffer.init(0); HTTPSClient *https = new HTTPSClient(m_socket->socket()); if (!https->init()){ m_socket->error_state("Can't initialize HTTPS"); return; } m_state = SSLConnect; m_socket->setSocket(https); https->connect(); https->process(); return; } #endif log(L_DEBUG, "HTTP connect ready"); m_socket->setRaw(true); m_socket->writeBuffer.packetStart(); string proto; string host; string user; string pass; string uri; string extra; unsigned short port; crackUrl(m_uri.c_str(), proto, host, port, user, pass, uri, extra); if (!extra.empty()){ uri += "?"; uri += extra; } m_socket->writeBuffer << (m_post ? "POST " : "GET ") << uri.c_str() << " HTTP/1.0\r\n"; if (!findHeader("Host")) m_socket->writeBuffer << "Host: " << host.c_str() << "\r\n"; if (!findHeader("User-Agent")) m_socket->writeBuffer << "User-Agent: " PACKAGE "/" VERSION "\r\n"; if (!findHeader("Authorization") && !user.empty()) m_socket->writeBuffer << "Authorization: basic " << basic_auth(user.c_str(), pass.c_str()).c_str() << "\r\n"; if (m_post){ if (!findHeader("Content-Length")) m_socket->writeBuffer << "Content-Length: " << number(m_post->size()).c_str() << "\r\n"; } for (HEADERS_MAP::iterator it = m_hOut.begin(); it != m_hOut.end(); ++it){ m_socket->writeBuffer << (*it).first.c_str() << ": " << (*it).second.c_str() << "\r\n"; } m_socket->writeBuffer << "\r\n"; if (m_post) m_socket->writeBuffer.pack(m_post->data(), m_post->size()); log_packet(m_socket->writeBuffer, true, HTTPPacket); m_socket->write(); m_socket->readBuffer.init(0); m_socket->readBuffer.packetStart(); }
HTTPAPI_RESULT HTTPAPI_ExecuteRequest(HTTP_HANDLE handle, HTTPAPI_REQUEST_TYPE requestType, const char* relativePath, HTTP_HEADERS_HANDLE httpHeadersHandle, const unsigned char* content, size_t contentLength, unsigned int* statusCode, HTTP_HEADERS_HANDLE responseHeadersHandle, BUFFER_HANDLE responseContent) { int result; size_t headersCount; char* header; HTTPSClient* client = (HTTPSClient*)handle; if (!client->connected()) { // client not connected LogError("HTTPS request failed, client not connected\n"); return HTTPAPI_OPEN_REQUEST_FAILED; } result = client->sendRequest(HTTPRequestTypes[requestType], relativePath); if (!result) { LogError("HTTPS send request failed\n"); return HTTPAPI_SEND_REQUEST_FAILED; } HTTPHeaders_GetHeaderCount(httpHeadersHandle, &headersCount); for (size_t i = 0; i < headersCount && result; i++) { HTTPHeaders_GetHeader(httpHeadersHandle, i, &header); result = client->sendHeader(header); free(header); } if (!result) { LogError("HTTPS send header failed\n"); return HTTPAPI_SEND_REQUEST_FAILED; } result = client->sendBody(content, contentLength); if (!result) { LogError("HTTPS send body failed\n"); return HTTPAPI_SEND_REQUEST_FAILED; } result = client->readStatus(); if (result == -1) { return HTTPAPI_STRING_PROCESSING_ERROR; } *statusCode = result; while (result > 0) { String headerName; String headerValue; result = client->readHeader(headerName, headerValue); HTTPHeaders_AddHeaderNameValuePair(responseHeadersHandle, headerName.c_str(), headerValue.c_str()); } if (result == -1) { LogError("HTTPS header parsing failed\n"); return HTTPAPI_HTTP_HEADERS_FAILED; } contentLength = client->contentLength(); if (contentLength) { BUFFER_pre_build(responseContent, contentLength); client->readBody(BUFFER_u_char(responseContent), contentLength); } return HTTPAPI_OK; }
void HTTPAPI_CloseConnection(HTTP_HANDLE handle) { HTTPSClient* client = (HTTPSClient*)handle; client->end(); }