void server_request::drop_body() { // Discard body content iff body stream exists if (body_stream_) { while (!body_stream().eof()) { char buf[1024]; body_stream().read(buf, sizeof(buf)); } body_stream_.reset(); restriction_.reset(); } }
/*---------------------------------------------------------------------- | PLT_HttpClient::WaitForResponse +---------------------------------------------------------------------*/ NPT_Result PLT_HttpClient::WaitForResponse(NPT_InputStreamReference& input_stream, NPT_HttpRequest& request, NPT_SocketInfo& info, NPT_HttpResponse*& response) { NPT_COMPILER_UNUSED(info); // create a buffered stream for this connection stream NPT_BufferedInputStreamReference buffered_input_stream(new NPT_BufferedInputStream(input_stream)); // parse the response NPT_CHECK(NPT_HttpResponse::Parse(*buffered_input_stream, response)); // unbuffer the stream buffered_input_stream->SetBufferSize(0); // create an entity if one is expected in the response if (request.GetMethod() == NPT_HTTP_METHOD_GET || request.GetMethod() == NPT_HTTP_METHOD_POST) { NPT_HttpEntity* response_entity = new NPT_HttpEntity(response->GetHeaders()); // Transfer-Encoding: chunked ? if (response_entity->GetTransferEncoding() == "chunked") { NPT_InputStreamReference body_stream(new NPT_HttpChunkedDecoderInputStream(buffered_input_stream)); response_entity->SetInputStream((NPT_InputStreamReference)body_stream); } else { response_entity->SetInputStream((NPT_InputStreamReference)buffered_input_stream); } response->SetEntity(response_entity); } return NPT_SUCCESS; }
void OutgoingMessage::writeToStream(QDataStream &stream) { // write body to a buffer so we can measure byte counts QByteArray message_body; QDataStream body_stream(&message_body, QIODevice::WriteOnly); writeMessageBody(body_stream); // write header const qint64 header_size = 9; qint8 _type = type(); stream << _type; qint64 byte_count = message_body.size() + header_size; stream << byte_count; // write body stream.device()->write(message_body); }
/*---------------------------------------------------------------------- | TestHttpPost +---------------------------------------------------------------------*/ static void TestHttpPost(const char* arg) { NPT_HttpUrl url(arg); NPT_HttpRequest request(url, NPT_HTTP_METHOD_POST); NPT_HttpClient client; NPT_HttpResponse* response; NPT_HttpEntity* body_entity = new NPT_HttpEntity(); NPT_InputStreamReference body_stream(new NPT_MemoryStream((void*)"hello blabla", 12)); body_entity->SetInputStream(body_stream); request.SetEntity(body_entity); NPT_Result result = client.SendRequest(request, response); NPT_Debug("SendRequest returned %d\n", result); if (NPT_FAILED(result)) return; ShowResponse(response); delete response; }
std::ostream& operator<<(const T& t) { body_stream() << t; return body_stream(); }
this_type& body(const T& t, const std::string& ct = common::content_type<T>::name) { content_type(ct); body_stream() << t; return *this; }
void SwitchboardServerConnection::sendInk(std::string image) { this->assertConnectionStateIsAtLeast(SB_READY); if(users.size() == 1) { p2p.sendInk(*this, image); // return; } std::string body("base64:"+image); bool one_packet = false; if(body.size() <= 1202) // if we need more than 1 packet, then use multipacket one_packet = true ; if(one_packet) { std::ostringstream buf_, msg_; msg_ << "MIME-Version: 1.0\r\n"; msg_ << "Content-Type: image/gif\r\n\r\n"; msg_ << body; size_t msg_length = msg_.str().size(); buf_ << "MSG " << this->trID++ << " N " << (int) msg_length << "\r\n" << msg_.str(); write(buf_); return; } else { std::istringstream body_stream(body); std::string messageid = new_branch(); std::vector<std::string> chunks; // spliting the message while(!body_stream.eof()) { char *part = new char[1203]; memset(part,0,1203); body_stream.read((char*)part, 1202); std::string part1(part); chunks.push_back(part1); delete [] part; } // sending the first one std::ostringstream buf_, msg_; msg_ << "MIME-Version: 1.0\r\n"; msg_ << "Content-Type: image/gif\r\n"; msg_ << "Message-ID: " << messageid << "\r\n"; msg_ << "Chunks: " << chunks.size() << "\r\n\r\n"; msg_ << chunks.front(); size_t msg_length = msg_.str().size(); buf_ << "MSG " << this->trID++ << " N " << (int) msg_length << "\r\n" << msg_.str(); write(buf_); std::vector<std::string>::iterator i = chunks.begin(); for(int num=0; i!=chunks.end(); i++, num++) { if(i == chunks.begin()) continue; std::ostringstream buf2_, msg2_; msg2_ << "Message-ID: " << messageid << "\r\n"; msg2_ << "Chunk: " << num << "\r\n\r\n"; msg2_ << (*i); size_t msg_length2 = msg2_.str().size(); buf2_ << "MSG " << this->trID++ << " N " << (int) msg_length2 << "\r\n" << msg2_.str(); write(buf2_); } } }