inline bool ClientImpl::write_request()

in aios/storage/indexlib/util/httplib.h [6600:6713]


inline bool ClientImpl::write_request(Stream& strm, Request& req, bool close_connection, Error& error)
{
    // Prepare additional headers
    if (close_connection) {
        if (!req.has_header("Connection")) {
            req.headers.emplace("Connection", "close");
        }
    }

    if (!req.has_header("Host")) {
        if (is_ssl()) {
            if (port_ == 443) {
                req.headers.emplace("Host", host_);
            } else {
                req.headers.emplace("Host", host_and_port_);
            }
        } else {
            if (port_ == 80) {
                req.headers.emplace("Host", host_);
            } else {
                req.headers.emplace("Host", host_and_port_);
            }
        }
    }

    if (!req.has_header("Accept")) {
        req.headers.emplace("Accept", "*/*");
    }

#ifndef CPPHTTPLIB_NO_DEFAULT_USER_AGENT
    if (!req.has_header("User-Agent")) {
        auto agent = std::string("cpp-httplib/") + CPPHTTPLIB_VERSION;
        req.headers.emplace("User-Agent", agent);
    }
#endif

    if (req.body.empty()) {
        if (req.content_provider_) {
            if (!req.is_chunked_content_provider_) {
                if (!req.has_header("Content-Length")) {
                    auto length = std::to_string(req.content_length_);
                    req.headers.emplace("Content-Length", length);
                }
            }
        } else {
            if (req.method == "POST" || req.method == "PUT" || req.method == "PATCH") {
                req.headers.emplace("Content-Length", "0");
            }
        }
    } else {
        if (!req.has_header("Content-Type")) {
            req.headers.emplace("Content-Type", "text/plain");
        }

        if (!req.has_header("Content-Length")) {
            auto length = std::to_string(req.body.size());
            req.headers.emplace("Content-Length", length);
        }
    }

    if (!basic_auth_password_.empty() || !basic_auth_username_.empty()) {
        if (!req.has_header("Authorization")) {
            req.headers.insert(make_basic_authentication_header(basic_auth_username_, basic_auth_password_, false));
        }
    }

    if (!proxy_basic_auth_username_.empty() && !proxy_basic_auth_password_.empty()) {
        if (!req.has_header("Proxy-Authorization")) {
            req.headers.insert(
                make_basic_authentication_header(proxy_basic_auth_username_, proxy_basic_auth_password_, true));
        }
    }

    if (!bearer_token_auth_token_.empty()) {
        if (!req.has_header("Authorization")) {
            req.headers.insert(make_bearer_token_authentication_header(bearer_token_auth_token_, false));
        }
    }

    if (!proxy_bearer_token_auth_token_.empty()) {
        if (!req.has_header("Proxy-Authorization")) {
            req.headers.insert(make_bearer_token_authentication_header(proxy_bearer_token_auth_token_, true));
        }
    }

    // Request line and headers
    {
        detail::BufferStream bstrm;

        const auto& path = url_encode_ ? detail::encode_url(req.path) : req.path;
        bstrm.write_format("%s %s HTTP/1.1\r\n", req.method.c_str(), path.c_str());

        detail::write_headers(bstrm, req.headers);

        // Flush buffer
        auto& data = bstrm.get_buffer();
        if (!detail::write_data(strm, data.data(), data.size())) {
            error = Error::Write;
            return false;
        }
    }

    // Body
    if (req.body.empty()) {
        return write_content_with_provider(strm, req, error);
    }

    if (!detail::write_data(strm, req.body.data(), req.body.size())) {
        error = Error::Write;
        return false;
    }

    return true;
}