///////////////////////////////////////////////////////
| 95 | |
| 96 | //////////////////////////////////////////////////////////// |
| 97 | std::string Http::Request::prepare() const |
| 98 | { |
| 99 | std::ostringstream out; |
| 100 | |
| 101 | // Convert the method to its string representation |
| 102 | std::string method; |
| 103 | switch (m_method) |
| 104 | { |
| 105 | case Method::Get: |
| 106 | method = "GET"; |
| 107 | break; |
| 108 | case Method::Post: |
| 109 | method = "POST"; |
| 110 | break; |
| 111 | case Method::Head: |
| 112 | method = "HEAD"; |
| 113 | break; |
| 114 | case Method::Put: |
| 115 | method = "PUT"; |
| 116 | break; |
| 117 | case Method::Delete: |
| 118 | method = "DELETE"; |
| 119 | break; |
| 120 | } |
| 121 | |
| 122 | // Write the first line containing the request type |
| 123 | out << method << " " << m_uri << " "; |
| 124 | out << "HTTP/" << m_majorVersion << "." << m_minorVersion << "\r\n"; |
| 125 | |
| 126 | // Write fields |
| 127 | for (const auto& [fieldKey, fieldValue] : m_fields) |
| 128 | { |
| 129 | out << fieldKey << ": " << fieldValue << "\r\n"; |
| 130 | } |
| 131 | |
| 132 | // Use an extra \r\n to separate the header from the body |
| 133 | out << "\r\n"; |
| 134 | |
| 135 | // Add the body |
| 136 | out << m_body; |
| 137 | |
| 138 | return out.str(); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | //////////////////////////////////////////////////////////// |