userver: http Namespace Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
http Namespace Reference

Detailed Description

HTTP helpers.

Namespaces

namespace  headers
 Common HTTP headers.
 

Classes

class  ContentType
 Content-Type representation. More...
 
class  ContentTypeHash
 
struct  DecomposedUrlView
 
class  MalformedContentType
 Content-Type parsing error. More...
 

Typedefs

using Args = std::unordered_map< std::string, std::string, utils::StrCaseHash >
 
using MultiArgs = std::multimap< std::string, std::string >
 
using PathArgs = std::unordered_map< std::string, std::string >
 

Enumerations

enum class  HttpVersion {
  kDefault ,
  k10 ,
  k11 ,
  k2 ,
  k2Tls ,
  k2PriorKnowledge
}
 HTTP version to use. More...
 
enum  StatusCode : uint16_t {
  kInvalid = 0 ,
  kContinue = 100 ,
  kSwitchingProtocols = 101 ,
  kProcessing = 102 ,
  kEarlyHints = 103 ,
  kOk = 200 ,
  kCreated = 201 ,
  kAccepted = 202 ,
  kNonAuthoritativeInformation = 203 ,
  kNoContent = 204 ,
  kResetContent = 205 ,
  kPartialContent = 206 ,
  kMultiStatus = 207 ,
  kAlreadyReported = 208 ,
  kThisIsFine = 218 ,
  kImUsed = 226 ,
  kMultipleChoices = 300 ,
  kMovedPermanently = 301 ,
  kFound = 302 ,
  kSeeOther = 303 ,
  kNotModified = 304 ,
  kUseProxy = 305 ,
  kSwitchProxy = 306 ,
  kTemporaryRedirect = 307 ,
  kPermanentRedirect = 308 ,
  kBadRequest = 400 ,
  kUnauthorized = 401 ,
  kPaymentRequired = 402 ,
  kForbidden = 403 ,
  kNotFound = 404 ,
  kMethodNotAllowed = 405 ,
  kNotAcceptable = 406 ,
  kProxyAuthenticationRequired = 407 ,
  kRequestTimeout = 408 ,
  kConflict = 409 ,
  kGone = 410 ,
  kLengthRequired = 411 ,
  kPreconditionFailed = 412 ,
  kPayloadTooLarge = 413 ,
  kUriTooLong = 414 ,
  kUnsupportedMediaType = 415 ,
  kRangeNotSatisfiable = 416 ,
  kExpectationFailed = 417 ,
  kImATeapot = 418 ,
  kPageExpired = 419 ,
  kMethodFailure = 420 ,
  kMisdirectedRequest = 421 ,
  kUnprocessableEntity = 422 ,
  kLocked = 423 ,
  kFailedDependency = 424 ,
  kTooEarly = 425 ,
  kUpgradeRequired = 426 ,
  kPreconditionRequired = 428 ,
  kTooManyRequests = 429 ,
  kRequestHeaderFieldsTooLarge = 431 ,
  kNginxNoResponse = 444 ,
  kUnavailableForLegalReasons = 451 ,
  kNginxRequestHeaderTooLarge = 494 ,
  kNginxSSLCertificateError = 495 ,
  kNginxSSLCertificateRequired = 496 ,
  kNginxHTTPRequestSenttoHTTPSPort = 497 ,
  kDeadlineExpired = 498 ,
  kNginxClientClosedRequest = 499 ,
  kInternalServerError = 500 ,
  kNotImplemented = 501 ,
  kBadGateway = 502 ,
  kServiceUnavailable = 503 ,
  kGatewayTimeout = 504 ,
  kHttpVersionNotSupported = 505 ,
  kVariantAlsoNegotiates = 506 ,
  kInsufficientStorage = 507 ,
  kLoopDetected = 508 ,
  kBandwidthLimitExceeded = 509 ,
  kNotExtended = 510 ,
  kNetworkAuthenticationRequired = 511 ,
  kWebServerIsDown = 520 ,
  kConnectionTimedOut = 522 ,
  kOriginIsUnreachable = 523 ,
  kTimeoutOccurred = 524 ,
  kSslHandshakeFailed = 525 ,
  kInvalidSslCertificate = 526 ,
  Invalid = kInvalid ,
  OK = kOk ,
  Created = kCreated ,
  NoContent = kNoContent ,
  BadRequest = kBadRequest ,
  NotFound = kNotFound ,
  Conflict = kConflict ,
  TooManyRequests = kTooManyRequests ,
  InternalServerError = kInternalServerError ,
  kClientClosedRequest = kNginxClientClosedRequest
}
 https://en.wikipedia.org/wiki/List_of_HTTP_status_codes More...
 

Functions

bool operator== (const ContentType &, const ContentType &)
 
bool operator!= (const ContentType &, const ContentType &)
 
bool operator< (const ContentType &, const ContentType &)
 
std::string_view ToString (HttpVersion version)
 
HttpVersion HttpVersionFromString (std::string_view version)
 
HttpVersion Parse (const yaml_config::YamlConfig &value, formats::parse::To< HttpVersion >)
 
std::string_view StatusCodeString (StatusCode status)
 
std::string ToString (StatusCode status)
 
std::ostream & operator<< (std::ostream &os, StatusCode s)
 
std::string UrlDecode (std::string_view range)
 Decode URL.
 
std::string UrlEncode (std::string_view input_string)
 Encode as URL.
 
std::string MakeQuery (const Args &query_args)
 Make an URL query.
 
std::string MakeQuery (const MultiArgs &query_args)
 Make an URL query.
 
std::string MakeQuery (const std::unordered_map< std::string, std::string > &query_args)
 Make an URL query.
 
std::string MakeQuery (std::initializer_list< std::pair< std::string_view, std::string_view > > query_args)
 Make an URL query.
 
std::string MakeUrl (std::string_view path, const Args &query_args)
 Make an URL with query arguments.
 
std::string MakeUrl (std::string_view path, const std::unordered_map< std::string, std::string > &query_args)
 Make an URL with query arguments.
 
std::string MakeUrl (std::string_view path, const Args &query_args, MultiArgs query_multiargs)
 Make an URL with query arguments.
 
std::string MakeUrl (std::string_view path, std::initializer_list< std::pair< std::string_view, std::string_view > > query_args)
 Make an URL with query arguments.
 
std::optional< std::string > MakeUrlWithPathArgs (std::string_view path, const PathArgs &path_args)
 Make a path from a template and arguments.
 
std::optional< std::string > MakeUrlWithPathArgs (std::string_view path, const PathArgs &path_args, const Args &query_args)
 Make an URL with path parameters and query arguments.
 
std::optional< std::string > MakeUrlWithPathArgs (std::string_view path, const PathArgs &path_args, const std::unordered_map< std::string, std::string > &query_args)
 Make an URL with path parameters and query arguments.
 
std::optional< std::string > MakeUrlWithPathArgs (std::string_view path, const PathArgs &path_args, const Args &query_args, MultiArgs query_multiargs)
 Make an URL with path parameters and query arguments, supporting multiple values for the same key.
 
std::optional< std::string > MakeUrlWithPathArgs (std::string_view path, const PathArgs &path_args, std::initializer_list< std::pair< std::string_view, std::string_view > > query_args)
 Make an URL with path parameters and query arguments.
 
std::string ExtractMetaTypeFromUrl (std::string_view url)
 Returns URL part before the first '?' character.
 
std::string_view ExtractMetaTypeFromUrlView (std::string_view url)
 
std::string ExtractPath (std::string_view url)
 Returns HTTP path part of a URL.
 
std::string_view ExtractPathView (std::string_view url)
 
std::string ExtractPathOnly (std::string_view url)
 Returns HTTP path part of a URL.
 
std::string ExtractHostname (std::string_view url)
 Returns hostname part of a URL.
 
std::string_view ExtractHostnameView (std::string_view url)
 
std::string ExtractScheme (std::string_view url)
 Returns scheme part of a URL.
 
std::string_view ExtractSchemeView (std::string_view url)
 
std::string ExtractQuery (std::string_view url)
 Returns query part of a URL.
 
std::string_view ExtractQueryView (std::string_view url)
 
std::string ExtractFragment (std::string_view url)
 Returns fragment part of a URL.
 
std::string_view ExtractFragmentView (std::string_view url)
 
DecomposedUrlView DecomposeUrlIntoViews (std::string_view url)
 Returns decomposed URL as a struct, broken into main parts: scheme, host, path, query, and fragment.
 
template<typename Value , typename = std::enable_if_t<formats::common::kIsFormatValue<Value>>>
StatusCode Parse (const Value &value, formats::parse::To< StatusCode >)
 

Typedef Documentation

◆ Args

using http::Args = typedef std::unordered_map<std::string, std::string, utils::StrCaseHash>

Definition at line 40 of file url.hpp.

◆ MultiArgs

using http::MultiArgs = typedef std::multimap<std::string, std::string>

Definition at line 41 of file url.hpp.

◆ PathArgs

using http::PathArgs = typedef std::unordered_map<std::string, std::string>

Definition at line 42 of file url.hpp.

Enumeration Type Documentation

◆ HttpVersion

enum class http::HttpVersion
strong

HTTP version to use.

Enumerator
kDefault 

unspecified version

k10 

HTTP/1.0 only.

k11 

HTTP/1.1 only.

k2 

HTTP/2 with fallback to HTTP/1.1.

k2Tls 

HTTP/2 over TLS only, otherwise (no TLS or h2) HTTP/1.1.

k2PriorKnowledge 

HTTP/2 only (without Upgrade)

Definition at line 15 of file http_version.hpp.

◆ StatusCode

Function Documentation

◆ ExtractFragment()

std::string http::ExtractFragment ( std::string_view  url)

Returns fragment part of a URL.

Parameters
urlFull URL to extract from
Returns
Fragment component of the URL
auto fragment = ExtractFragment("https://example.com/api/users?q=1");
// Returns: ""
auto fragment2 = ExtractFragment("http://user:pass@example.com:8080/api#123");
// Returns: "123"
auto fragment3 = ExtractFragment("ftp://[::1]:8080/#123?q=12&w=23");
// Returns: "123"
auto fragment4 = ExtractFragment("ftp://[::1]:8080/?q=12&w=23#123");
// Returns: "123"

◆ ExtractHostname()

std::string http::ExtractHostname ( std::string_view  url)

Returns hostname part of a URL.

Parameters
urlFull URL to extract from
Returns
Hostname component of the URL
auto host = ExtractHostname("https://example.com/api/users");
// Returns: "example.com"
auto host2 = ExtractHostname("https://user:pass@example.com:8080/api");
// Returns: "example.com"
auto host3 = ExtractHostname("http://[::1]:8080/");
// Returns: "[::1]"

◆ ExtractMetaTypeFromUrl()

std::string http::ExtractMetaTypeFromUrl ( std::string_view  url)

Returns URL part before the first '?' character.

Parameters
urlFull URL to extract from
Returns
URL without query string
auto base = ExtractMetaTypeFromUrl("https://example.com/api/users?page=1&sort=name");
// Returns: "https://example.com/api/users"

◆ ExtractPath()

std::string http::ExtractPath ( std::string_view  url)

Returns HTTP path part of a URL.

Parameters
urlFull URL to extract from
Returns
Path component of the URL
auto path = ExtractPath("https://example.com/api/users");
// Returns: "/api/users"
auto path2 = ExtractPath("example.com/api/users?a=b");
// Returns: "/api/users?a=b"

◆ ExtractPathOnly()

std::string http::ExtractPathOnly ( std::string_view  url)

Returns HTTP path part of a URL.

Parameters
urlFull URL to extract from
Returns
Path component of the URL
auto path = ExtractPath("https://example.com/api/users");
// Returns: "/api/users"
auto path2 = ExtractPath("example.com/api/users?a=b");
// Returns: "/api/users"

◆ ExtractQuery()

std::string http::ExtractQuery ( std::string_view  url)

Returns query part of a URL.

Parameters
urlFull URL to extract from
Returns
Query component of the URL
auto query = ExtractQuery("https://example.com/api/users?q=1");
// Returns: "q=1"
auto query2 = ExtractQuery("http://user:pass@example.com:8080/api");
// Returns: ""
auto query3 = ExtractQuery("ftp://[::1]:8080/?q=12&w=23");
// Returns: "q=12&w=23"

◆ ExtractScheme()

std::string http::ExtractScheme ( std::string_view  url)

Returns scheme part of a URL.

Parameters
urlFull URL to extract from
Returns
Scheme component of the URL
auto scheme = ExtractScheme("https://example.com/api/users");
// Returns: "https"
auto scheme2 = ExtractScheme("http://user:pass@example.com:8080/api");
// Returns: "http"
auto scheme3 = ExtractScheme("ftp://[::1]:8080/");
// Returns: "ftp"

◆ MakeQuery() [1/4]

std::string http::MakeQuery ( const Args &  query_args)

Make an URL query.

Parameters
query_argsMap of query parameters
Returns
URL query string without leading '?' character
auto query = MakeQuery(http::Args{{"param", "value"}, {"filter", "active"}});
// Returns: "param=value&filter=active"

◆ MakeQuery() [2/4]

std::string http::MakeQuery ( const MultiArgs &  query_args)

Make an URL query.

Parameters
query_argsMultimap of query parameters
Returns
URL query string without leading '?' character
http::MultiArgs args = {{"tag", "new"}, {"tag", "featured"}};
auto query = MakeQuery(args);
// Returns: "tag=new&tag=featured"

◆ MakeQuery() [3/4]

std::string http::MakeQuery ( const std::unordered_map< std::string, std::string > &  query_args)

Make an URL query.

Parameters
query_argsMap of query parameters
Returns
URL query string without leading '?' character
auto query = MakeQuery(std::unordered_map<std::string, std::string>{{"page", "1"}, {"size", "10"}});
// Returns: "page=1&size=10"

◆ MakeQuery() [4/4]

std::string http::MakeQuery ( std::initializer_list< std::pair< std::string_view, std::string_view > >  query_args)

Make an URL query.

Parameters
query_argsInitializer list of query parameters as key-value pairs
Returns
URL query string without leading '?' character
auto query = MakeQuery({{"sort", "date"}, {"order", "desc"}});
// Returns: "sort=date&order=desc"

◆ MakeUrl() [1/4]

std::string http::MakeUrl ( std::string_view  path,
const Args &  query_args 
)

Make an URL with query arguments.

Parameters
pathBase URL path
query_argsMap of query parameters
Returns
Complete URL with query string
auto url = MakeUrl("/api/users", http::Args{{"status", "active"}});
// Returns: "/api/users?status=active"
Examples
samples/http_caching/main.cpp.

◆ MakeUrl() [2/4]

std::string http::MakeUrl ( std::string_view  path,
const Args &  query_args,
MultiArgs  query_multiargs 
)

Make an URL with query arguments.

Parameters
pathBase URL path
query_argsMap of query parameters
query_multiargsMultimap for query parameters that can have multiple values
Returns
Complete URL with query string
http::MultiArgs multi_args = {{"tag", "new"}, {"tag", "featured"}};
auto url = MakeUrl("/api/products", http::Args{{"category", "electronics"}}, multi_args);
// Returns: "/api/products?category=electronics&tag=new&tag=featured"

◆ MakeUrl() [3/4]

std::string http::MakeUrl ( std::string_view  path,
const std::unordered_map< std::string, std::string > &  query_args 
)

Make an URL with query arguments.

Parameters
pathBase URL path
query_argsMap of query parameters
Returns
Complete URL with query string
auto url = MakeUrl("/api/products", std::unordered_map<std::string, std::string>{{"category", "electronics"}});
// Returns: "/api/products?category=electronics"

◆ MakeUrl() [4/4]

std::string http::MakeUrl ( std::string_view  path,
std::initializer_list< std::pair< std::string_view, std::string_view > >  query_args 
)

Make an URL with query arguments.

Parameters
pathBase URL path
query_argsInitializer list of query parameters as key-value pairs
Returns
Complete URL with query string
auto url = MakeUrl("/api/search", {{"q", "smartphone"}, {"sort", "relevance"}});
// Returns: "/api/search?q=smartphone&sort=relevance"

◆ MakeUrlWithPathArgs() [1/5]

std::optional< std::string > http::MakeUrlWithPathArgs ( std::string_view  path,
const PathArgs &  path_args 
)

Make a path from a template and arguments.

Parameters
pathTemplate string with placeholders in format {name}
path_argsMap of placeholder names to their values
Returns
Formatted path or std::nullopt if formatting fails (e.g., missing placeholder, invalid format, or empty key in path_args)
auto url = MakeUrlWithPathArgs("/api/v1/users/{user_id}", {{"user_id", "123"}});
// Returns: "/api/v1/users/123"

◆ MakeUrlWithPathArgs() [2/5]

std::optional< std::string > http::MakeUrlWithPathArgs ( std::string_view  path,
const PathArgs &  path_args,
const Args &  query_args 
)

Make an URL with path parameters and query arguments.

Parameters
pathTemplate string with placeholders in format {name}
path_argsMap of placeholder names to their values
query_argsMap of query parameters
Returns
Formatted URL or std::nullopt if path formatting fails
auto url = MakeUrlWithPathArgs("/api/v1/users/{user_id}",
{{"user_id", "123"}},
http::Args{{"filter", "active"}});
// Returns: "/api/v1/users/123?filter=active"

◆ MakeUrlWithPathArgs() [3/5]

std::optional< std::string > http::MakeUrlWithPathArgs ( std::string_view  path,
const PathArgs &  path_args,
const Args &  query_args,
MultiArgs  query_multiargs 
)

Make an URL with path parameters and query arguments, supporting multiple values for the same key.

Parameters
pathTemplate string with placeholders in format {name}
path_argsMap of placeholder names to their values
query_argsMap of query parameters
query_multiargsMultimap for query parameters that can have multiple values
Returns
Formatted URL or std::nullopt if path formatting fails
http::MultiArgs multi_args = {{"tag", "new"}, {"tag", "featured"}};
auto url = MakeUrlWithPathArgs("/api/v1/products/{category}",
{{"category", "electronics"}},
http::Args{{"sort", "price"}},
multi_args);
// Returns: "/api/v1/products/electronics?sort=price&tag=new&tag=featured"

◆ MakeUrlWithPathArgs() [4/5]

std::optional< std::string > http::MakeUrlWithPathArgs ( std::string_view  path,
const PathArgs &  path_args,
const std::unordered_map< std::string, std::string > &  query_args 
)

Make an URL with path parameters and query arguments.

Parameters
pathTemplate string with placeholders in format {name}
path_argsMap of placeholder names to their values
query_argsMap of query parameters
Returns
Formatted URL or std::nullopt if path formatting fails
auto url = MakeUrlWithPathArgs("/api/v1/users/{user_id}",
{{"user_id", "123"}},
std::unordered_map<std::string, std::string>{{"page", "1"}});
// Returns: "/api/v1/users/123?page=1"

◆ MakeUrlWithPathArgs() [5/5]

std::optional< std::string > http::MakeUrlWithPathArgs ( std::string_view  path,
const PathArgs &  path_args,
std::initializer_list< std::pair< std::string_view, std::string_view > >  query_args 
)

Make an URL with path parameters and query arguments.

Parameters
pathTemplate string with placeholders in format {name}
path_argsMap of placeholder names to their values
query_argsInitializer list of query parameters as key-value pairs
Returns
Formatted URL or std::nullopt if path formatting fails
auto url = MakeUrlWithPathArgs("/api/v1/search/{term}",
{{"term", "laptop"}},
{{"brand", "apple"}, {"price_max", "2000"}});
// Returns: "/api/v1/search/laptop?brand=apple&price_max=2000"

◆ operator<()

bool http::operator< ( const ContentType ,
const ContentType  
)

Weak ordering for Accept media-ranges checking. Positions less specific types before more specific, so that the most specific type can be matched first.

◆ Parse()

template<typename Value , typename = std::enable_if_t<formats::common::kIsFormatValue<Value>>>
StatusCode http::Parse ( const Value &  value,
formats::parse::To< StatusCode  
)

Definition at line 16 of file parse_status_code.hpp.

◆ UrlEncode()

std::string http::UrlEncode ( std::string_view  input_string)

Encode as URL.

Parameters
input_stringString to encode
Returns
URL-encoded string where special characters are encoded as XX sequences
auto encoded = UrlEncode("hello world");
// Returns: "hello%20world"