void IppURLConnection::getContent() { if (connected) { if (getResponseCode() == HTTP_OK && is_contenttype_ipp(getContentType())) { istream &is = getInputStream(); is >> *__ippResponse; } else {
// static shared_ptr <POP3Response> POP3Response::readMultilineResponse(shared_ptr <POP3Connection> conn) { shared_ptr <POP3Response> resp = shared_ptr <POP3Response> (new POP3Response(conn->getSocket(), conn->getTimeoutHandler(), conn->getTracer())); string buffer; resp->readResponseImpl(buffer, /* multiLine */ true); string firstLine, nextLines; stripFirstLine(buffer, nextLines, &firstLine); resp->m_firstLine = firstLine; resp->m_code = getResponseCode(firstLine); stripResponseCode(firstLine, resp->m_text); std::istringstream iss(nextLines); string line; if (resp->m_tracer) resp->m_tracer->traceReceive(firstLine); while (std::getline(iss, line, '\n')) { line = utility::stringUtils::trim(line); resp->m_lines.push_back(line); if (resp->m_tracer) resp->m_tracer->traceReceive(line); } if (resp->m_tracer) resp->m_tracer->traceReceive("."); return resp; }
ci::BufferRef UrlLoader::loadUrl( const std::string& url ) { ci::BufferRef result; mUrl = url; if( JniHelper::Get()->AttachCurrentThread() ) { jstring jstrUrl = JniHelper::Get()->NewStringUTF( mUrl ); jbyteArray jBytes = (jbyteArray)JniHelper::Get()->CallObjectMethod( mJavaObject, UrlLoader::sJavaMethodLoadUrl, jstrUrl ); if( nullptr == jBytes ) { int resCode = getResponseCode(); std::string resMsg = getResponseMsg(); std::string excMsg = getExceptionMsg(); std::stringstream ss; ss << "UrlLoader.loadUrl returned null (Response Code: " << resCode << ", Response Msg: " << resMsg << ", Exception Msg: " << excMsg << ")"; throw std::runtime_error( ss.str() ); } size_t dataLength = JniHelper::Get()->GetArrayLength( jBytes ); jbyte* dataPtr = (jbyte*)JniHelper::Get()->GetByteArrayElements( jBytes, NULL ); if( nullptr == dataPtr ) { int resCode = getResponseCode(); std::string resMsg = getResponseMsg(); std::string excMsg = getExceptionMsg(); std::stringstream ss; ss << "failed to get byte array (Response Code: " << resCode << ", Response Msg: " << resMsg << ", Exception Msg: " << excMsg << ")"; throw std::runtime_error( ss.str() ); } if( dataLength > 0 ) { result = ci::Buffer::create( dataLength ); memcpy( (void *)result->getData(), (const void *)dataPtr, dataLength ); } JniHelper::Get()->ReleaseByteArrayElements( jBytes, dataPtr, 0 ); JniHelper::Get()->DeleteLocalRef( jstrUrl ); } return result; }
RoString RoDeleteCharacterCancelResponse::getResponseDescription() const { switch (getResponseCode()) { case Code::SUCCESS: return RoString{ L"Character deletion request was successfully canceled." }; case Code::ERROR_DATABASE: return RoString{ L"There was a problem at the server-side that prevented canceling character deletion." }; default: break; } return RoString{ L"There was an unknown error when trying to cancel the character deletion." }; }
String Nimbits::getFullResponse(EthernetClient client) { char c; String response; while(client.connected() && !client.available()) delay(1); while (client.available()) { c = client.read(); response += c; } Serial.println(response); int responseCode = getResponseCode(response); return response; }
bool HttpRequest::send() { mError = ""; // check that TNL understands the supplied address if(!mRemoteAddress->isValid()) { mError = "Address invalid"; return false; } S32 connectError = mSocket->connect(*mRemoteAddress); if(connectError == UnknownError) { mError = "Connect error"; return false; } if(!mSocket->isWritable(FIVE_SECONDS)) { mError = "Socket not writable"; return false; } buildRequest(); if(!sendRequest(mRequest)) { mError = "Can't send request"; return false; } string response = receiveResponse(); if(response == "") { mError = "No response"; return false; } parseResponse(response); if(getResponseCode() == 0) { mError = "Invalid response code"; return false; } return true; }
// static shared_ptr <POP3Response> POP3Response::readResponse(shared_ptr <POP3Connection> conn) { shared_ptr <POP3Response> resp = shared_ptr <POP3Response> (new POP3Response(conn->getSocket(), conn->getTimeoutHandler(), conn->getTracer())); string buffer; resp->readResponseImpl(buffer, /* multiLine */ false); PDBG0( "read buffer %s", buffer.c_str() ); resp->m_firstLine = buffer; resp->m_code = getResponseCode(buffer); stripResponseCode(buffer, resp->m_text); if (resp->m_tracer) resp->m_tracer->traceReceive(buffer); return resp; }
// static shared_ptr <POP3Response> POP3Response::readLargeResponse (shared_ptr <POP3Connection> conn, utility::outputStream& os, utility::progressListener* progress, const size_t predictedSize) { shared_ptr <POP3Response> resp = shared_ptr <POP3Response> (new POP3Response(conn->getSocket(), conn->getTimeoutHandler(), conn->getTracer())); string firstLine; const size_t length = resp->readResponseImpl(firstLine, os, progress, predictedSize); resp->m_firstLine = firstLine; resp->m_code = getResponseCode(firstLine); stripResponseCode(firstLine, resp->m_text); if (resp->m_tracer) { resp->m_tracer->traceReceive(firstLine); resp->m_tracer->traceReceiveBytes(length - firstLine.length()); resp->m_tracer->traceReceive("."); } return resp; }
int CURLNetRequest::getResponseCode(CURLcode err, Vector<char> const &body, IRhoSession* oSession) { return getResponseCode(err, &body[0], body.size(), oSession); }
int CURLNetRequest::getResponseCode(CURLcode err, String const &body, IRhoSession* oSession) { return getResponseCode(err, body.c_str(), body.size(), oSession); }
INetResponse* CURLNetRequest::pushMultipartData(const String& strUrl, VectorPtr<CMultipartItem*>& arItems, IRhoSession* oSession, Hashtable<String,String>* pHeaders) { int nRespCode = -1; String strRespBody; RAWLOG_INFO1("POST request (Push): %s", strUrl.c_str()); rho_net_impl_network_indicator(1); curl_slist *hdrs = m_curl.set_options("POST", strUrl, String(), oSession, pHeaders); CURL *curl = m_curl.curl(); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strRespBody); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &curlBodyStringCallback); curl_httppost *post = NULL, *last = NULL; curl_easy_setopt(curl, CURLOPT_POSTFIELDS, NULL); for (size_t i = 0, lim = arItems.size(); i < lim; ++i) { CMultipartItem *mi = arItems[i]; size_t cl; if (mi->m_strFilePath.empty()) cl = mi->m_strBody.size(); else { common::CRhoFile f; if (!f.open(mi->m_strFilePath.c_str(), common::CRhoFile::OpenReadOnly)) cl = 0; else { cl = f.size(); f.close(); } } char buf[32]; buf[sizeof(buf) - 1] = '\0'; snprintf(buf, sizeof(buf) - 1, "Content-Length: %lu", (unsigned long)cl); curl_slist *fh = NULL; fh = curl_slist_append(fh, buf); const char *name = mi->m_strName.empty() ? "blob" : mi->m_strName.c_str(); int opt = mi->m_strFilePath.empty() ? CURLFORM_COPYCONTENTS : CURLFORM_FILE; const char *data = mi->m_strFilePath.empty() ? mi->m_strBody.c_str() : mi->m_strFilePath.c_str(); const char *ct = mi->m_strContentType.empty() ? NULL : mi->m_strContentType.c_str(); if (ct) { curl_formadd(&post, &last, CURLFORM_COPYNAME, name, opt, data, CURLFORM_CONTENTTYPE, ct, CURLFORM_CONTENTHEADER, fh, CURLFORM_END); } else { curl_formadd(&post, &last, CURLFORM_COPYNAME, name, opt, data, CURLFORM_CONTENTHEADER, fh, CURLFORM_END); } } curl_easy_setopt(curl, CURLOPT_HTTPPOST, post); CURLcode err = doCURLPerform(strUrl); curl_slist_free_all(hdrs); curl_formfree(post); rho_net_impl_network_indicator(0); nRespCode = getResponseCode(err, strRespBody, oSession); return makeResponse(strRespBody, nRespCode); }
INetResponse* CURLNetRequest::doPull(const char* method, const String& strUrl, const String& strBody, common::CRhoFile *oFile, IRhoSession* oSession, Hashtable<String,String>* pHeaders ) { int nRespCode = -1; Vector<char> respBody; long nStartFrom = 0; if (oFile) nStartFrom = oFile->size(); if( !net::URI::isLocalHost(strUrl.c_str()) ) { //Log every non localhost requests RAWLOG_INFO2("%s request (Pull): %s", method, strUrl.c_str()); rho_net_impl_network_indicator(1); } Hashtable<String,String> h; if (pHeaders) h = *pHeaders; for (int nAttempts = 0; nAttempts < 10; ++nAttempts) { Vector<char> respChunk; curl_slist *hdrs = m_curl.set_options(method, strUrl, strBody, oSession, &h); CURL *curl = m_curl.curl(); if (pHeaders) { curl_easy_setopt(curl, CURLOPT_HEADERDATA, pHeaders); curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, &curlHeaderCallback); } curl_easy_setopt(curl, CURLOPT_WRITEDATA, &respChunk); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &curlBodyBinaryCallback); if (nStartFrom > 0) curl_easy_setopt(curl, CURLOPT_RESUME_FROM, nStartFrom); CURLcode err = doCURLPerform(strUrl); curl_slist_free_all(hdrs); long statusCode = 0; if (curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &statusCode) != 0) statusCode = 500; if (statusCode == 416 ) { //Do nothing, file is already loaded }else if (statusCode == 206) { if (oFile) oFile->write(&respChunk[0], respChunk.size()); else std::copy(respChunk.begin(), respChunk.end(), std::back_inserter(respBody)); // Clear counter of attempts because 206 response does not considered to be failed attempt nAttempts = 0; } else { if (oFile) { oFile->movePosToStart(); oFile->write(&respChunk[0], respChunk.size()); } else respBody = respChunk; } if (err == CURLE_OPERATION_TIMEDOUT && respChunk.size() > 0) { RAWTRACE("Connection was closed by timeout, but we have part of data received; try to restore connection"); nStartFrom = oFile ? oFile->size() : respBody.size(); continue; } nRespCode = getResponseCode(err, respBody, oSession); break; } if( !net::URI::isLocalHost(strUrl.c_str()) ) rho_net_impl_network_indicator(0); return makeResponse(respBody, nRespCode); }
HTTPResponse *HTTPClient::httpPostFile(std::string URL, std::string file) { ssize_t fileSize = Utils::fileSize(file.c_str()); if (fileSize == -1) return NULL; FILE *fp = fopen(file.c_str(), "r"); if (fp == NULL) { // Woops! return NULL; } uint8_t *buf = (uint8_t *) malloc(fileSize); memset(buf, 0, (size_t) fileSize); fread(buf, fileSize, 1, fp); fclose(fp); HTTPResponse *resp = new HTTPResponse(); Tcp client; char serverName[100]; unsigned short serverPort = 80; size_t pathOffset = hostFromURL(URL.c_str(), serverName, &serverPort); if (client.connectTo(std::string(serverName), serverPort)) { Log::d("Connect to server OK"); char linebuf[1024]; snprintf(linebuf, 1024, "POST %s HTTP/1.1", URL.c_str() + pathOffset); client.println(linebuf); if (serverPort != 80) { snprintf(linebuf, 1024, "Host: %s:%i", serverName, serverPort); } else { snprintf(linebuf, 1024, "Host: %s", serverName); } client.println(linebuf); client.println("Content-Type: application/octet-stream"); client.println("Connection: close"); std::string contentLength = "Content-Length: " + Utils::toString((int) fileSize); client.println(contentLength.c_str()); client.println(""); client.send(buf, fileSize); Log::d("HTTP Request to %s sent", URL.c_str()); // Request sent, wait for reply unsigned long reqTime = Utils::millis(); while (!client.available() && (Utils::millis() - reqTime < HTTP_RESPONSE_TIMEOUT_VALUE)) { #ifndef NOWATCHDOG Watchdog::heartBeat(); #endif } if (client.available()) { char rBuffer[300 + 1]; memset(rBuffer, 0, 300 + 1); int s = getLine(client, (uint8_t *) rBuffer, 300); Log::d("buffer response[%i]: %s", s, rBuffer); if (strncmp(rBuffer, "HTTP/1.", 7) == 0) { resp->error = HTTP_OK; resp->responseCode = getResponseCode(rBuffer); // Read headers do { s = getLine(client, (uint8_t *) rBuffer, 300); if (s > 0 && strlen(rBuffer) != 0) { char *dppos = strchr(rBuffer, ':'); *dppos = 0; if (*(dppos + 1) == ' ') { dppos++; } dppos++; resp->headers[std::string(rBuffer)] = std::string(dppos); } } while (s > 0 && strlen(rBuffer) != 0); resp->body = NULL; if (resp->headers.count("Content-Length") == 1) { size_t bodySize = (size_t) atol(resp->headers["Content-Length"].c_str()); resp->body = (uint8_t *) malloc(bodySize + 1); memset(resp->body, 0, bodySize + 1); client.readall(resp->body, bodySize); } } else { Log::e("HTTP malformed reply"); resp->error = HTTP_MALFORMED_REPLY; } } else { Log::e("HTTP request timeout"); resp->error = HTTP_REQUEST_TIMEOUT; } } else { Log::e("HTTP connection timeout"); resp->error = HTTP_CONNECTION_TIMEOUT; } free(buf); Log::d("Stopping tcp client"); client.stop(); // TODO: better handling? while (client.connected()) { client.stop(); } return resp; }
HTTPResponse *HTTPClient::httpRequest(HTTPMethod method, std::string URL, std::map<std::string, std::string> postValues) { HTTPResponse *resp = new HTTPResponse(); Tcp client; char serverName[100]; unsigned short serverPort = 80; size_t pathOffset = hostFromURL(URL.c_str(), serverName, &serverPort); if (client.connectTo(std::string(serverName), serverPort)) { Log::d("Connect to server OK"); char linebuf[1024]; snprintf(linebuf, 1024, "%s %s HTTP/1.1", (method == HTTP_GET ? "GET" : "POST"), URL.c_str() + pathOffset); client.println(linebuf); if (serverPort != 80) { snprintf(linebuf, 1024, "Host: %s:%i", serverName, serverPort); } else { snprintf(linebuf, 1024, "Host: %s", serverName); } client.println(linebuf); client.println("Connection: close"); if (method == HTTP_POST && postValues.empty()) { client.println("Content-Length: 0"); client.println(""); } else if (method == HTTP_POST && !postValues.empty()) { std::string reqBody; for (std::map<std::string, std::string>::iterator i = postValues.begin(); i != postValues.end(); ++i) { reqBody.append(i->first); reqBody.append("="); reqBody.append(i->second); reqBody.append("&"); } reqBody = Utils::trim(reqBody, '&'); client.println("Content-Type: application/x-www-form-urlencoded"); unsigned long contentLength = reqBody.size(); snprintf(linebuf, 1024, "Content-Length: %lu", contentLength); client.println(linebuf); client.println(""); client.print(reqBody.c_str()); Log::d("HTTP Post: %s", reqBody.c_str()); } Log::d("HTTP Request to %s sent", URL.c_str()); // Request sent, wait for reply unsigned long reqTime = Utils::millis(); while (!client.available() && (Utils::millis() - reqTime < HTTP_RESPONSE_TIMEOUT_VALUE)) { ; } if (client.available()) { char rBuffer[300 + 1]; memset(rBuffer, 0, 300 + 1); int s = getLine(client, (uint8_t *) rBuffer, 300); Log::d("buffer response[%i]: %s", s, rBuffer); if (strncmp(rBuffer, "HTTP/1.", 7) == 0) { resp->error = HTTP_OK; resp->responseCode = getResponseCode(rBuffer); // Read headers do { s = getLine(client, (uint8_t *) rBuffer, 300); if (s > 0 && strlen(rBuffer) != 0) { char *dppos = strchr(rBuffer, ':'); *dppos = 0; if (*(dppos + 1) == ' ') { dppos++; } dppos++; resp->headers[std::string(rBuffer)] = std::string(dppos); } } while (s > 0 && strlen(rBuffer) != 0); resp->body = NULL; if (resp->headers.count("Content-Length") == 1) { size_t bodySize = (size_t) atol(resp->headers["Content-Length"].c_str()); resp->body = (uint8_t *) malloc(bodySize + 1); memset(resp->body, 0, bodySize + 1); client.readall(resp->body, bodySize); } } else { Log::e("HTTP malformed reply"); resp->error = HTTP_MALFORMED_REPLY; } } else { Log::e("HTTP request timeout"); resp->error = HTTP_REQUEST_TIMEOUT; } } else { Log::e("HTTP connection timeout"); resp->error = HTTP_CONNECTION_TIMEOUT; } Log::d("Stopping tcp client"); client.stop(); // TODO: better handling? while (client.connected()) { client.stop(); } return resp; }
size_t POP3Response::readResponseImpl (string& firstLine, utility::outputStream& os, utility::progressListener* progress, const size_t predictedSize) { size_t current = 0, total = predictedSize; string temp; bool codeDone = false; if (progress) progress->start(total); if (m_timeoutHandler) m_timeoutHandler->resetTimeOut(); utility::inputStreamSocketAdapter sis(*m_socket); utility::stopSequenceFilteredInputStream <5> sfis1(sis, "\r\n.\r\n"); utility::stopSequenceFilteredInputStream <3> sfis2(sfis1, "\n.\n"); utility::dotFilteredInputStream dfis(sfis2); // "\n.." --> "\n." utility::inputStream& is = dfis; while (!is.eof()) { // Check whether the time-out delay is elapsed if (m_timeoutHandler && m_timeoutHandler->isTimeOut()) { if (!m_timeoutHandler->handleTimeOut()) throw exceptions::operation_timed_out(); } // Receive data from the socket byte_t buffer[65536]; const size_t read = is.read(buffer, sizeof(buffer)); if (read == 0) // buffer is empty { if (m_socket->getStatus() & socket::STATUS_WANT_WRITE) { m_socket->waitForWrite(); } else if (m_socket->getStatus() & socket::STATUS_WANT_READ) { m_socket->waitForRead(); } else { // Input stream needs more bytes to continue, but there // is enough data into socket buffer. Do not waitForRead(), // just retry read()ing on the stream. } continue; } // We have received data: reset the time-out counter if (m_timeoutHandler) m_timeoutHandler->resetTimeOut(); // Notify progress current += read; if (progress) { total = std::max(total, current); progress->progress(current, total); } // If we don't have extracted the response code yet if (!codeDone) { vmime::utility::stringUtils::appendBytesToString(temp, buffer, read); string responseData; if (stripFirstLine(temp, responseData, &firstLine) == true) { PDBG0( "firstLine: %s", firstLine.c_str() ); if (getResponseCode(firstLine) != CODE_OK) throw exceptions::command_error("?", firstLine); codeDone = true; os.write(responseData.data(), responseData.length()); temp.clear(); continue; } } else { // Inject the data into the output stream os.write(buffer, read); } } if (progress) progress->stop(total); return current; }