Beispiel #1
0
void FileServerRequestHandler::sendError(int errorCode, const Poco::Net::HTTPRequest& request,
                                         const std::shared_ptr<StreamSocket>& socket,
                                         const std::string& shortMessage, const std::string& longMessage,
                                         const std::string& extraHeader)
{
    Poco::URI requestUri(request.getURI());
    const std::string& path = requestUri.getPath();
    std::ostringstream oss;
    oss << "HTTP/1.1 " << errorCode << "\r\n"
        << "Content-Type: text/html charset=UTF-8\r\n"
        << "Date: " << Poco::DateTimeFormatter::format(Poco::Timestamp(), Poco::DateTimeFormat::HTTP_FORMAT) << "\r\n"
        << "User-Agent: " << WOPI_AGENT_STRING << "\r\n"
        << extraHeader
        << "\r\n";
    if (!shortMessage.empty())
    {
        oss << "<h1>Error: " << shortMessage << "</h1>"
            << "<p>" << longMessage << " " << path << "</p>"
            << "<p>Please contact your system administrator.</p>";
    }
    socket->send(oss.str());
}
Beispiel #2
0
void HttpAssetTransfer::SendHttpPostAssetRequest(const std::string &host, const std::string &json_data)
{
    std::string ASSET_UPLOADING_CONTENT_TYPE = "application/json";
    int content_length = json_data.length();
    std::istringstream stream(json_data);

    int send_count = 0; //\todo make member var

    Poco::URI uri(host);
    std::string path(uri.getPathAndQuery());
    if (path.empty())
        path = "/";

    http_session_.setHost(uri.getHost());
    http_session_.setPort(uri.getPort());

    Poco::Timespan time_out(HTTP_TIMEOUT_MS*1000);

    Poco::Net::HTTPRequest request;
    request.setMethod(Poco::Net::HTTPRequest::HTTP_POST);
    std::string t = uri.toString();
    request.setURI(uri.getPath());
    request.setVersion(Poco::Net::HTTPMessage::HTTP_1_1);
    request.setContentType(ASSET_UPLOADING_CONTENT_TYPE);
    request.setContentLength(content_length);
    request.setKeepAlive(false);

    try
    {
        std::ostream &request_body = http_session_.sendRequest(request);
        request_body.write(json_data.c_str(), json_data.length());

        std::istream &s = http_session_.receiveResponse(http_response_);

        Poco::Net::HTTPResponse::HTTPStatus status = http_response_.getStatus();
        switch (status)
        {
        case Poco::Net::HTTPResponse::HTTP_OK:
            response_stream_ = &s;
            response_size_ = http_response_.getContentLength();
            break;

        default:
            std::string reason = http_response_.getReasonForStatus(status);
            std::stringstream error;
            error << "Http POST failed for: ";
            error << host << std::endl;
            error << "Reason: " << reason;
            AssetModule::LogError(error.str());
            failed_ = true;
            return;
        }
    }
    catch (Poco::Net::MessageException &e)
    {
        std::string u = request.getURI();
        std::string m = request.getMethod();
        std::string error = e.message();

    }
    catch (Poco::Exception &e)
    {
        std::stringstream error;
        error << "Http POST failed for: ";
        error << host << std::endl;
        error << "Reason: " << e.displayText();
        AssetModule::LogError(error.str());
        failed_ = true;
        return;
    }
}