1*ec865cb8SNiels Sascha Reedijk/* 2*ec865cb8SNiels Sascha Reedijk * Copyright 2022 Haiku, Inc. All rights reserved. 3*ec865cb8SNiels Sascha Reedijk * Distributed under the terms of the MIT License. 4*ec865cb8SNiels Sascha Reedijk * 5*ec865cb8SNiels Sascha Reedijk * Authors: 6*ec865cb8SNiels Sascha Reedijk * Niels Sascha Reedijk, niels.reedijk@gmail.com 7*ec865cb8SNiels Sascha Reedijk * 8*ec865cb8SNiels Sascha Reedijk * Corresponds to: 9*ec865cb8SNiels Sascha Reedijk * headers/private/netservices2/HttpRequest.h hrev????? 10*ec865cb8SNiels Sascha Reedijk * src/kits/network/libnetservices2/HttpRequest.cpp hrev????? 11*ec865cb8SNiels Sascha Reedijk */ 12*ec865cb8SNiels Sascha Reedijk 13*ec865cb8SNiels Sascha Reedijk 14*ec865cb8SNiels Sascha Reedijk#if __cplusplus >= 201703L 15*ec865cb8SNiels Sascha Reedijk 16*ec865cb8SNiels Sascha Reedijk 17*ec865cb8SNiels Sascha Reedijk/*! 18*ec865cb8SNiels Sascha Reedijk \file HttpRequest.h 19*ec865cb8SNiels Sascha Reedijk \ingroup netservices 20*ec865cb8SNiels Sascha Reedijk \brief Provides the classes and tools to build HTTP Requests. 21*ec865cb8SNiels Sascha Reedijk 22*ec865cb8SNiels Sascha Reedijk \since Haiku R1 23*ec865cb8SNiels Sascha Reedijk*/ 24*ec865cb8SNiels Sascha Reedijk 25*ec865cb8SNiels Sascha Reedijk 26*ec865cb8SNiels Sascha Reedijknamespace BPrivate { 27*ec865cb8SNiels Sascha Reedijk 28*ec865cb8SNiels Sascha Reedijknamespace Network { 29*ec865cb8SNiels Sascha Reedijk 30*ec865cb8SNiels Sascha Reedijk 31*ec865cb8SNiels Sascha Reedijk/*! 32*ec865cb8SNiels Sascha Reedijk \class BHttpMethod 33*ec865cb8SNiels Sascha Reedijk \ingroup netservices 34*ec865cb8SNiels Sascha Reedijk \brief Represent a HTTP method. 35*ec865cb8SNiels Sascha Reedijk 36*ec865cb8SNiels Sascha Reedijk The <a href="https://datatracker.ietf.org/doc/html/rfc7231#section-4.1">HTTP standard</a> 37*ec865cb8SNiels Sascha Reedijk specifies that HTTP requests have a method. Common methods are \c GET and \c HEAD methods. 38*ec865cb8SNiels Sascha Reedijk Standardized and common methods are in the form of \em verbs and are in capitalized letters 39*ec865cb8SNiels Sascha Reedijk from the ASCII token set, though any valid token can be used. 40*ec865cb8SNiels Sascha Reedijk 41*ec865cb8SNiels Sascha Reedijk It is most likely that you will not use the methods of this class directly, instead you will 42*ec865cb8SNiels Sascha Reedijk use the implicit constructors while interacting with the \ref BHttpRequest class. 43*ec865cb8SNiels Sascha Reedijk 44*ec865cb8SNiels Sascha Reedijk \code 45*ec865cb8SNiels Sascha Reedijk auto url = BUrl2("https://www.haiku-os.org/"); 46*ec865cb8SNiels Sascha Reedijk // implicitly construct a standard get request 47*ec865cb8SNiels Sascha Reedijk auto standard = BHttpRequest(url, BHttpMethod::Get); 48*ec865cb8SNiels Sascha Reedijk // implicitly construct a nonstandard patch request 49*ec865cb8SNiels Sascha Reedijk auto custom = BHttpRequest(url, "PATCH"sv); 50*ec865cb8SNiels Sascha Reedijk \endcode 51*ec865cb8SNiels Sascha Reedijk 52*ec865cb8SNiels Sascha Reedijk \note When you are using the standard list of verbs, there will never be an exception when 53*ec865cb8SNiels Sascha Reedijk creating objects of this type. When you create a custom method, exceptions may be raised 54*ec865cb8SNiels Sascha Reedijk when the system runs out of memory, or when your custom method contains invalid characters. 55*ec865cb8SNiels Sascha Reedijk In almost all cases, you can probably safely assume you will not run into these exceptions, 56*ec865cb8SNiels Sascha Reedijk except for cases where you use user input to create methods or you are very defensive 57*ec865cb8SNiels Sascha Reedijk about memory management. 58*ec865cb8SNiels Sascha Reedijk 59*ec865cb8SNiels Sascha Reedijk \since Haiku R1 60*ec865cb8SNiels Sascha Reedijk*/ 61*ec865cb8SNiels Sascha Reedijk 62*ec865cb8SNiels Sascha Reedijk 63*ec865cb8SNiels Sascha Reedijk/*! 64*ec865cb8SNiels Sascha Reedijk \class BHttpMethod::InvalidMethod 65*ec865cb8SNiels Sascha Reedijk \ingroup netservices 66*ec865cb8SNiels Sascha Reedijk \brief Error that represents when a custom method does not conform to the HTTP standard. 67*ec865cb8SNiels Sascha Reedijk 68*ec865cb8SNiels Sascha Reedijk \since Haiku R1 69*ec865cb8SNiels Sascha Reedijk*/ 70*ec865cb8SNiels Sascha Reedijk 71*ec865cb8SNiels Sascha Reedijk 72*ec865cb8SNiels Sascha Reedijk/*! 73*ec865cb8SNiels Sascha Reedijk \var BString BHttpMethod::InvalidMethod::input 74*ec865cb8SNiels Sascha Reedijk \brief The input that contains the invalid contents. 75*ec865cb8SNiels Sascha Reedijk 76*ec865cb8SNiels Sascha Reedijk \since Haiku R1 77*ec865cb8SNiels Sascha Reedijk*/ 78*ec865cb8SNiels Sascha Reedijk 79*ec865cb8SNiels Sascha Reedijk 80*ec865cb8SNiels Sascha Reedijk/*! 81*ec865cb8SNiels Sascha Reedijk \fn BHttpMethod::InvalidMethod::InvalidMethod(const char *origin, BString input) 82*ec865cb8SNiels Sascha Reedijk \brief Constructor that sets the \a origin and the invalid \a input. 83*ec865cb8SNiels Sascha Reedijk 84*ec865cb8SNiels Sascha Reedijk \since Haiku R1 85*ec865cb8SNiels Sascha Reedijk*/ 86*ec865cb8SNiels Sascha Reedijk 87*ec865cb8SNiels Sascha Reedijk 88*ec865cb8SNiels Sascha Reedijk/*! 89*ec865cb8SNiels Sascha Reedijk \enum BHttpMethod::Verb 90*ec865cb8SNiels Sascha Reedijk \ingroup netservices 91*ec865cb8SNiels Sascha Reedijk \brief A list of standard HTTP methods. 92*ec865cb8SNiels Sascha Reedijk 93*ec865cb8SNiels Sascha Reedijk \since Haiku R1 94*ec865cb8SNiels Sascha Reedijk*/ 95*ec865cb8SNiels Sascha Reedijk 96*ec865cb8SNiels Sascha Reedijk 97*ec865cb8SNiels Sascha Reedijk/*! 98*ec865cb8SNiels Sascha Reedijk \var BHttpMethod::Verb BHttpMethod::Get 99*ec865cb8SNiels Sascha Reedijk \brief Represents the \c GET method. 100*ec865cb8SNiels Sascha Reedijk 101*ec865cb8SNiels Sascha Reedijk \since Haiku R1 102*ec865cb8SNiels Sascha Reedijk*/ 103*ec865cb8SNiels Sascha Reedijk 104*ec865cb8SNiels Sascha Reedijk 105*ec865cb8SNiels Sascha Reedijk/*! 106*ec865cb8SNiels Sascha Reedijk \var BHttpMethod::Verb BHttpMethod::Head 107*ec865cb8SNiels Sascha Reedijk \brief Represents the \c HEAD method. 108*ec865cb8SNiels Sascha Reedijk 109*ec865cb8SNiels Sascha Reedijk \since Haiku R1 110*ec865cb8SNiels Sascha Reedijk*/ 111*ec865cb8SNiels Sascha Reedijk 112*ec865cb8SNiels Sascha Reedijk 113*ec865cb8SNiels Sascha Reedijk/*! 114*ec865cb8SNiels Sascha Reedijk \var BHttpMethod::Verb BHttpMethod::Post 115*ec865cb8SNiels Sascha Reedijk \brief Represents the \c POST method. 116*ec865cb8SNiels Sascha Reedijk 117*ec865cb8SNiels Sascha Reedijk \since Haiku R1 118*ec865cb8SNiels Sascha Reedijk*/ 119*ec865cb8SNiels Sascha Reedijk 120*ec865cb8SNiels Sascha Reedijk 121*ec865cb8SNiels Sascha Reedijk/*! 122*ec865cb8SNiels Sascha Reedijk \var BHttpMethod::Verb BHttpMethod::Put 123*ec865cb8SNiels Sascha Reedijk \brief Represents the \c PUT method. 124*ec865cb8SNiels Sascha Reedijk 125*ec865cb8SNiels Sascha Reedijk \since Haiku R1 126*ec865cb8SNiels Sascha Reedijk*/ 127*ec865cb8SNiels Sascha Reedijk 128*ec865cb8SNiels Sascha Reedijk 129*ec865cb8SNiels Sascha Reedijk/*! 130*ec865cb8SNiels Sascha Reedijk \var BHttpMethod::Verb BHttpMethod::Delete 131*ec865cb8SNiels Sascha Reedijk \brief Represents the \c DELETE method. 132*ec865cb8SNiels Sascha Reedijk 133*ec865cb8SNiels Sascha Reedijk \since Haiku R1 134*ec865cb8SNiels Sascha Reedijk*/ 135*ec865cb8SNiels Sascha Reedijk 136*ec865cb8SNiels Sascha Reedijk 137*ec865cb8SNiels Sascha Reedijk/*! 138*ec865cb8SNiels Sascha Reedijk \var BHttpMethod::Verb BHttpMethod::Connect 139*ec865cb8SNiels Sascha Reedijk \brief Represents the \c CONNECT method. 140*ec865cb8SNiels Sascha Reedijk 141*ec865cb8SNiels Sascha Reedijk \since Haiku R1 142*ec865cb8SNiels Sascha Reedijk*/ 143*ec865cb8SNiels Sascha Reedijk 144*ec865cb8SNiels Sascha Reedijk 145*ec865cb8SNiels Sascha Reedijk/*! 146*ec865cb8SNiels Sascha Reedijk \var BHttpMethod::Verb BHttpMethod::Options 147*ec865cb8SNiels Sascha Reedijk \brief Represents the \c OPTIONS method. 148*ec865cb8SNiels Sascha Reedijk 149*ec865cb8SNiels Sascha Reedijk \since Haiku R1 150*ec865cb8SNiels Sascha Reedijk*/ 151*ec865cb8SNiels Sascha Reedijk 152*ec865cb8SNiels Sascha Reedijk 153*ec865cb8SNiels Sascha Reedijk/*! 154*ec865cb8SNiels Sascha Reedijk \var BHttpMethod::Verb BHttpMethod::Trace 155*ec865cb8SNiels Sascha Reedijk \brief Represents the \c TRACE method. 156*ec865cb8SNiels Sascha Reedijk 157*ec865cb8SNiels Sascha Reedijk \since Haiku R1 158*ec865cb8SNiels Sascha Reedijk*/ 159*ec865cb8SNiels Sascha Reedijk 160*ec865cb8SNiels Sascha Reedijk 161*ec865cb8SNiels Sascha Reedijk/*! 162*ec865cb8SNiels Sascha Reedijk \fn BHttpMethod::BHttpMethod(BHttpMethod &&other) noexcept 163*ec865cb8SNiels Sascha Reedijk \brief Move constructor. 164*ec865cb8SNiels Sascha Reedijk 165*ec865cb8SNiels Sascha Reedijk Moves the data from the \a other to this object. The \a other object will be set to 166*ec865cb8SNiels Sascha Reedijk \ref BHttpMethod::Get. 167*ec865cb8SNiels Sascha Reedijk 168*ec865cb8SNiels Sascha Reedijk \since Haiku R1 169*ec865cb8SNiels Sascha Reedijk*/ 170*ec865cb8SNiels Sascha Reedijk 171*ec865cb8SNiels Sascha Reedijk 172*ec865cb8SNiels Sascha Reedijk/*! 173*ec865cb8SNiels Sascha Reedijk \fn BHttpMethod::BHttpMethod(const BHttpMethod &other) 174*ec865cb8SNiels Sascha Reedijk \brief Copy constructor. 175*ec865cb8SNiels Sascha Reedijk 176*ec865cb8SNiels Sascha Reedijk Copy data from an \a other object. 177*ec865cb8SNiels Sascha Reedijk 178*ec865cb8SNiels Sascha Reedijk \exception std::bad_alloc When the \a other object contains a custom verb, this exception 179*ec865cb8SNiels Sascha Reedijk will be raised if it is impossible to allocate memory. 180*ec865cb8SNiels Sascha Reedijk 181*ec865cb8SNiels Sascha Reedijk \since Haiku R1 182*ec865cb8SNiels Sascha Reedijk*/ 183*ec865cb8SNiels Sascha Reedijk 184*ec865cb8SNiels Sascha Reedijk 185*ec865cb8SNiels Sascha Reedijk/*! 186*ec865cb8SNiels Sascha Reedijk \fn BHttpMethod::BHttpMethod(const std::string_view &method) 187*ec865cb8SNiels Sascha Reedijk \brief Construct a custom method. 188*ec865cb8SNiels Sascha Reedijk 189*ec865cb8SNiels Sascha Reedijk \param method The verb for the method. 190*ec865cb8SNiels Sascha Reedijk 191*ec865cb8SNiels Sascha Reedijk \exception std::bad_alloc In case it is not possible to allocate memory for the custom string. 192*ec865cb8SNiels Sascha Reedijk \exception BHttpMethod::InvalidMethod In case the \a method is empty or contains invalid 193*ec865cb8SNiels Sascha Reedijk characters. 194*ec865cb8SNiels Sascha Reedijk 195*ec865cb8SNiels Sascha Reedijk \since Haiku R1 196*ec865cb8SNiels Sascha Reedijk*/ 197*ec865cb8SNiels Sascha Reedijk 198*ec865cb8SNiels Sascha Reedijk 199*ec865cb8SNiels Sascha Reedijk/*! 200*ec865cb8SNiels Sascha Reedijk \fn BHttpMethod::BHttpMethod(Verb verb) noexcept 201*ec865cb8SNiels Sascha Reedijk \brief Construct a standard method. 202*ec865cb8SNiels Sascha Reedijk 203*ec865cb8SNiels Sascha Reedijk \param verb The chosen method. 204*ec865cb8SNiels Sascha Reedijk 205*ec865cb8SNiels Sascha Reedijk \since Haiku R1 206*ec865cb8SNiels Sascha Reedijk*/ 207*ec865cb8SNiels Sascha Reedijk 208*ec865cb8SNiels Sascha Reedijk 209*ec865cb8SNiels Sascha Reedijk/*! 210*ec865cb8SNiels Sascha Reedijk \fn BHttpMethod::~BHttpMethod() 211*ec865cb8SNiels Sascha Reedijk \brief Destructor. 212*ec865cb8SNiels Sascha Reedijk 213*ec865cb8SNiels Sascha Reedijk \since Haiku R1 214*ec865cb8SNiels Sascha Reedijk*/ 215*ec865cb8SNiels Sascha Reedijk 216*ec865cb8SNiels Sascha Reedijk 217*ec865cb8SNiels Sascha Reedijk/*! 218*ec865cb8SNiels Sascha Reedijk \fn const std::string_view BHttpMethod::Method() const noexcept 219*ec865cb8SNiels Sascha Reedijk \brief Get a string representation of the method. 220*ec865cb8SNiels Sascha Reedijk 221*ec865cb8SNiels Sascha Reedijk \return A \c std::string_view that is a string representation of the standard or custom method 222*ec865cb8SNiels Sascha Reedijk in this object. The lifetime of the string view is bound to the lifetime of this method. 223*ec865cb8SNiels Sascha Reedijk 224*ec865cb8SNiels Sascha Reedijk \since Haiku R1 225*ec865cb8SNiels Sascha Reedijk*/ 226*ec865cb8SNiels Sascha Reedijk 227*ec865cb8SNiels Sascha Reedijk 228*ec865cb8SNiels Sascha Reedijk/*! 229*ec865cb8SNiels Sascha Reedijk \fn BHttpMethod& BHttpMethod::operator=(BHttpMethod &&other) noexcept 230*ec865cb8SNiels Sascha Reedijk \brief Move assignment. 231*ec865cb8SNiels Sascha Reedijk Moves the data from the \a other to this object. The \a other object will be set to 232*ec865cb8SNiels Sascha Reedijk \ref BHttpMethod::Get. 233*ec865cb8SNiels Sascha Reedijk 234*ec865cb8SNiels Sascha Reedijk \since Haiku R1 235*ec865cb8SNiels Sascha Reedijk*/ 236*ec865cb8SNiels Sascha Reedijk 237*ec865cb8SNiels Sascha Reedijk 238*ec865cb8SNiels Sascha Reedijk/*! 239*ec865cb8SNiels Sascha Reedijk \fn BHttpMethod& BPrivate::Network::BHttpMethod::operator=(const BHttpMethod &other) 240*ec865cb8SNiels Sascha Reedijk \brief Copy assignment. 241*ec865cb8SNiels Sascha Reedijk 242*ec865cb8SNiels Sascha Reedijk Copy data from an \a other object. 243*ec865cb8SNiels Sascha Reedijk 244*ec865cb8SNiels Sascha Reedijk \exception std::bad_alloc When the \a other object contains a custom verb, this exception 245*ec865cb8SNiels Sascha Reedijk will be raised if it is impossible to allocate memory. 246*ec865cb8SNiels Sascha Reedijk 247*ec865cb8SNiels Sascha Reedijk \since Haiku R1 248*ec865cb8SNiels Sascha Reedijk*/ 249*ec865cb8SNiels Sascha Reedijk 250*ec865cb8SNiels Sascha Reedijk 251*ec865cb8SNiels Sascha Reedijk} // namespace Network 252*ec865cb8SNiels Sascha Reedijk 253*ec865cb8SNiels Sascha Reedijk} // namespace BPrivate 254*ec865cb8SNiels Sascha Reedijk 255*ec865cb8SNiels Sascha Reedijk#endif 256