void SimpleHttpResult::addHeaderField (std::string const& key, std::string const& value) { string k = StringUtils::trim(StringUtils::tolower(key)); if (k == "http/1.1" || k == "http/1.0") { if (value.length() > 2) { // we assume the status code is 3 chars long string code = value.substr(0, 3); setHttpReturnCode(atoi(code.c_str())); setHttpReturnMessage(value.substr(3 + 1)); } } else if (k == "content-length") { setContentLength((size_t) StringUtils::int64(value.c_str())); } else if (k == "transfer-encoding") { if (StringUtils::tolower(value) == "chunked") { _chunked = true; } } else if (k == "content-encoding") { if (StringUtils::tolower(value) == "deflate") { _deflated = true; } } _headerFields[k] = value; }
void HexView::setModel(HexModel *model) { model_ = model; qDebug() << "size=" << model->size(); connect(model,SIGNAL(sizeChanged()),this,SLOT(sizeChanged())); setContentLength(model->size()); }
void HTTPServerResponseImpl::sendFile(const std::string& path, const std::string& mediaType) { poco_assert (!_pStream); File f(path); Timestamp dateTime = f.getLastModified(); File::FileSize length = f.getSize(); set("Last-Modified", DateTimeFormatter::format(dateTime, DateTimeFormat::HTTP_FORMAT)); #if defined(POCO_HAVE_INT64) setContentLength64(length); #else setContentLength(static_cast<int>(length)); #endif setContentType(mediaType); setChunkedTransferEncoding(false); Poco::FileInputStream istr(path); if (istr.good()) { _pStream = new HTTPHeaderOutputStream(_session); write(*_pStream); if (_pRequest && _pRequest->getMethod() != HTTPRequest::HTTP_HEAD) { StreamCopier::copyStream(istr, *_pStream); } } else throw OpenFileException(path); }
static int prepRequest(HttpConn *conn, MprList *files, int retry) { MprKeyValue *header; char *seq; int next; httpPrepClientConn(conn, retry); for (next = 0; (header = mprGetNextItem(app->headers, &next)) != 0; ) { if (scaselessmatch(header->key, "User-Agent")) { httpSetHeader(conn, header->key, header->value); } else { httpAppendHeader(conn, header->key, header->value); } } if (app->text) { httpSetHeader(conn, "Accept", "text/plain"); } if (app->sequence) { static int next = 0; seq = itos(next++); httpSetHeader(conn, "X-Http-Seq", seq); } if (app->ranges) { httpSetHeader(conn, "Range", app->ranges); } if (app->formData) { httpSetHeader(conn, "Content-Type", "application/x-www-form-urlencoded"); } if (setContentLength(conn, files) < 0) { return MPR_ERR_CANT_OPEN; } return 0; }
void PostRequest::prepareRequest() { _form.prepareSubmit(*this); // \TODO Fix for Poco Bug // https://github.com/pocoproject/poco/issues/1331 if (!getChunkedTransferEncoding()) { // TODO: This long thing is a work-around for bug https://github.com/pocoproject/poco/issues/1337 setContentLength(_form.getEncoding() == Poco::Net::HTMLForm::ENCODING_URL ? 0 : _form.calculateContentLength()); } }
void HTTPServerResponseImpl::redirect(const std::string& uri, HTTPStatus status) { poco_assert (!_pStream); setContentLength(0); setChunkedTransferEncoding(false); setStatusAndReason(status); set("Location", uri); _pStream = new HTTPHeaderOutputStream(_session); write(*_pStream); }
void HTTPServerResponseImpl::sendBuffer(const void* pBuffer, std::size_t length) { poco_assert (!_pStream); setContentLength(static_cast<int>(length)); setChunkedTransferEncoding(false); _pStream = new HTTPHeaderOutputStream(_session); write(*_pStream); if (_pRequest && _pRequest->getMethod() != HTTPRequest::HTTP_HEAD) { _pStream->write(static_cast<const char*>(pBuffer), static_cast<std::streamsize>(length)); } }
/*! * Универсальный метод для отдачи тела фида. */ bool FeedHandler::serveFeed(ChatChannel channel, const QString &feedName) { if (!ifMethodAllowed()) return true; if (!channel || !channel->feeds().all().contains(feedName)) { setNoStore(); m_response->writeHead(Tufao::HttpServerResponse::NOT_FOUND); m_response->end(); return true; } FeedPtr feed = channel->feed(feedName); qint64 date = feed->head().date(); RestReplyCache &cache = m_cache[channel->id() + feedName.toUtf8()]; if (cache.date != date) { cache.date = date; cache.etag = etag(date, m_path.toUtf8()); cache.body = JSON::generate(feed->feed()); } setLastModified(date); setETag(cache.etag); setNoCache(); if (!ifModified(cache.etag)) { m_response->writeHead(Tufao::HttpServerResponse::NOT_MODIFIED); m_response->end(); return true; } m_response->writeHead(Tufao::HttpServerResponse::OK); if (m_request->method() != "HEAD") { setContentLength(cache.body.size()); m_response->end(cache.body); } else m_response->end(); return true; }
HexView::HexView() : showGuidelines_(false),model_(0) { setBytesPerRow( 16 ); setRowHeight( 20 ); // setContentLength(0); setShowAscii(true); setShowHex(true); setShowOffsets(true); setOffsetsWidth(100); setSpacer(35); setHexSpacer(25); setAsciiSpacer(15); setOffsetLen(6); setContentLength(273); recalcHeight(); recalcWidth(); setRenderTarget(QQuickPaintedItem::FramebufferObject); }
QByteArray HttpResponseGenerator::toByteArray() { // A gzip seems to have 23 bytes overhead. // Also "content-encoding: gzip\r\n" is 26 bytes long // So we only benefit from gzip if the message is bigger than 23+26 = 49 // If the message is smaller than 49 bytes we actually send MORE data if we gzip if (m_gzip && m_message.size() > 49) { QByteArray dest_buf; if (gCompress(dest_buf)) { setValue("content-encoding", "gzip"); #if QT_VERSION < 0x040800 m_message = dest_buf; #else m_message.swap(dest_buf); #endif } } setContentLength(m_message.size()); return QHttpResponseHeader::toString().toUtf8() + m_message; }
void HttpResponseGenerator::setMessage(const QByteArray& message) { HttpResponseGenerator::message = message; setContentLength(message.size()); }
void SimpleHttpResult::addHeaderField (char const* key, size_t keyLength, char const* value, size_t valueLength) { // trim key char const* end = key + keyLength; while (key < end && (*key == ' ' || *key == '\t')) { ++key; --keyLength; } // lower-case key std::string keyString(key, keyLength); StringUtils::tolowerInPlace(&keyString); // trim value end = value + valueLength; while (value < end && (*value == ' ' || *value == '\t')) { ++value; --valueLength; } if (keyString[0] == 'h') { if (! _foundHeader && (keyString == "http/1.1" || keyString == "http/1.0")) { if (valueLength > 2) { _foundHeader = true; // we assume the status code is 3 chars long if ((value[0] >= '0' && value[0] <= '9') && (value[1] >= '0' && value[1] <= '9') && (value[2] >= '0' && value[2] <= '9')) { // set response code setHttpReturnCode(100 * (value[0] - '0') + 10 * (value[1] - '0') + (value[2] - '0')); } if (valueLength >= 4) { setHttpReturnMessage(std::string(value + 4, valueLength - 4)); } } } } else if (keyString[0] == 'c') { if (keyString.size() == strlen("content-length") && keyString == "content-length") { setContentLength((size_t) StringUtils::int64(value, valueLength)); } else if (keyString.size() == strlen("content-encoding") && keyString == "content-encoding") { if (valueLength == strlen("deflate") && (value[0] == 'd' || value[0] == 'D') && (value[1] == 'e' || value[1] == 'E') && (value[2] == 'f' || value[2] == 'F') && (value[3] == 'l' || value[3] == 'L') && (value[4] == 'a' || value[4] == 'A') && (value[5] == 't' || value[5] == 'T') && (value[6] == 'e' || value[6] == 'E')) { _deflated = true; } } } else if (keyString[0] == 't') { if (keyString.size() == strlen("transfer-encoding") && keyString == "transfer-encoding") { if (valueLength == strlen("chunked") && (value[0] == 'c' || value[0] == 'C') && (value[1] == 'h' || value[1] == 'H') && (value[2] == 'u' || value[2] == 'U') && (value[3] == 'n' || value[3] == 'N') && (value[4] == 'k' || value[4] == 'K') && (value[5] == 'e' || value[5] == 'E') && (value[6] == 'd' || value[6] == 'D')) { _chunked = true; } } } auto result = _headerFields.emplace(keyString, std::string(value, valueLength)); if (! result.second) { // header already present _headerFields[keyString] = std::string(value, valueLength); } }
static int doRequest(MprHttp *http, cchar *url, MprList *fields, MprList *files) { MprHttpResponse *resp; MprKeyValue *header; char buf[MPR_HTTP_BUFSIZE], seqBuf[16], *responseHeaders, *redirect; int64 contentLen; int code, next, count, bytes, transCount; mprAssert(url && *url); mprLog(http, MPR_DEBUG, "fetch: %s %s", method, url); /* * Send the request */ count = -1; transCount = 0; do { for (next = 0; (header = mprGetNextItem(headers, &next)) != 0; ) { mprSetHttpHeader(http, 0, header->key, header->value); } if (sequence) { static int next = 0; mprItoa(seqBuf, sizeof(seqBuf), next++, 10); mprSetHttpHeader(http, 1, "X-Http-Seq", seqBuf); } if (ranges) { mprSetHttpHeader(http, 1, "Range", ranges); } if (fields) { mprSetHttpHeader(http, 1, "Content-Type", "application/x-www-form-urlencoded"); } if (chunkSize) { mprSetHttpChunked(http, 1); } if (setContentLength(http, fields, files) < 0) { return MPR_ERR_CANT_OPEN; } if (mprStartHttpRequest(http, method, url) < 0) { mprError(http, "Can't process request for \"%s\". %s", url, mprGetHttpError(http)); return MPR_ERR_CANT_OPEN; } /* * This program does not do full-duplex writes with reads. ie. if you have a request that sends and receives * data in parallel -- http will do the writes first then read the response. */ if (files || fields) { if (writeBody(http, fields, files) < 0) { mprError(http, "Can't write body data to \"%s\". %s", url, mprGetHttpError(http)); return MPR_ERR_CANT_WRITE; } } else { if (chunkSize) { mprFinalizeHttpWriting(http); } mprWaitForHttpResponse(http, -1); } #if WIN _setmode(fileno(stdout), O_BINARY); #endif while ((bytes = mprReadHttp(http, buf, sizeof(buf))) > 0) { showOutput(http, buf, bytes); } mprWaitForHttp(http, MPR_HTTP_STATE_COMPLETE, -1); /* May not be complete if a disconnect occurs */ if (http->state >= MPR_HTTP_STATE_CONTENT) { if (mprNeedHttpRetry(http, &redirect)) { if (redirect) { url = resolveUrl(http, redirect); } count--; transCount++; continue; } break; } } while (++count < http->retries && transCount < 4 && !mprIsExiting(http)); if (count >= http->retries) { mprError(http, "http: failed \"%s\" request for %s after %d attempt(s)", method, url, count); return MPR_ERR_TOO_MANY; } if (mprIsExiting(http)) { return MPR_ERR_BAD_STATE; } /* * Request now complete */ code = mprGetHttpCode(http); contentLen = mprGetHttpContentLength(http); mprLog(http, 6, "Response code %d, content len %d", code, contentLen); if (http->response) { if (showCode) { mprPrintf(http, "%d\n", code); } if (showHeaders) { responseHeaders = mprGetHttpHeaders(http); resp = http->response; mprPrintf(http, "%s %d %s\n", resp->protocol, resp->code, resp->message); mprPrintf(http, "%s\n", responseHeaders); mprFree(responseHeaders); } } if (code < 0) { mprError(http, "Can't process request for \"%s\" %s", url, mprGetHttpError(http)); return MPR_ERR_CANT_READ; } else if (code == 0 && http->protocolVersion == 0) { ; } else if (!(200 <= code && code <= 206) && !(301 <= code && code <= 304)) { if (!showCode) { mprError(http, "Can't process request for \"%s\" (%d) %s", url, code, mprGetHttpError(http)); return MPR_ERR_CANT_READ; } } lock(); if (verbose && noout) { trace(http, url, fetchCount, method, code, (int) contentLen); } unlock(); return 0; }
EIoBufferDecoder::EIoBufferDecoder(int contentLength) { ctx = new DecodingContext(); setContentLength(contentLength, false); }