| 1 | // |
| 2 | // HTTPMessage.h |
| 3 | // |
| 4 | // Library: Net |
| 5 | // Package: HTTP |
| 6 | // Module: HTTPMessage |
| 7 | // |
| 8 | // Definition of the HTTPMessage class. |
| 9 | // |
| 10 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
| 11 | // and Contributors. |
| 12 | // |
| 13 | // SPDX-License-Identifier: BSL-1.0 |
| 14 | // |
| 15 | |
| 16 | |
| 17 | #ifndef Net_HTTPMessage_INCLUDED |
| 18 | #define Net_HTTPMessage_INCLUDED |
| 19 | |
| 20 | |
| 21 | #include "Poco/Net/Net.h" |
| 22 | #include "Poco/Net/MessageHeader.h" |
| 23 | |
| 24 | |
| 25 | namespace Poco { |
| 26 | namespace Net { |
| 27 | |
| 28 | |
| 29 | class MediaType; |
| 30 | |
| 31 | |
| 32 | class Net_API HTTPMessage: public MessageHeader |
| 33 | /// The base class for HTTPRequest and HTTPResponse. |
| 34 | /// |
| 35 | /// Defines the common properties of all HTTP messages. |
| 36 | /// These are version, content length, content type |
| 37 | /// and transfer encoding. |
| 38 | { |
| 39 | public: |
| 40 | void setVersion(const std::string& version); |
| 41 | /// Sets the HTTP version for this message. |
| 42 | |
| 43 | const std::string& getVersion() const; |
| 44 | /// Returns the HTTP version for this message. |
| 45 | |
| 46 | void setContentLength(std::streamsize length); |
| 47 | /// Sets the Content-Length header. |
| 48 | /// |
| 49 | /// If length is UNKNOWN_CONTENT_LENGTH, removes |
| 50 | /// the Content-Length header. |
| 51 | |
| 52 | std::streamsize getContentLength() const; |
| 53 | /// Returns the content length for this message, |
| 54 | /// which may be UNKNOWN_CONTENT_LENGTH if |
| 55 | /// no Content-Length header is present. |
| 56 | |
| 57 | #if defined(POCO_HAVE_INT64) |
| 58 | void setContentLength64(Poco::Int64 length); |
| 59 | /// Sets the Content-Length header. |
| 60 | /// |
| 61 | /// If length is UNKNOWN_CONTENT_LENGTH, removes |
| 62 | /// the Content-Length header. |
| 63 | /// |
| 64 | /// In contrast to setContentLength(), this method takes |
| 65 | /// a 64-bit integer as content length. |
| 66 | |
| 67 | Poco::Int64 getContentLength64() const; |
| 68 | /// Returns the content length for this message, |
| 69 | /// which may be UNKNOWN_CONTENT_LENGTH if |
| 70 | /// no Content-Length header is present. |
| 71 | /// |
| 72 | /// In contrast to getContentLength(), this method |
| 73 | /// always returns a 64-bit integer for content length. |
| 74 | #endif // defined(POCO_HAVE_INT64) |
| 75 | |
| 76 | bool hasContentLength() const; |
| 77 | /// Returns true iff a Content-Length header is present. |
| 78 | |
| 79 | void setTransferEncoding(const std::string& transferEncoding); |
| 80 | /// Sets the transfer encoding for this message. |
| 81 | /// |
| 82 | /// The value should be either IDENTITY_TRANSFER_CODING |
| 83 | /// or CHUNKED_TRANSFER_CODING. |
| 84 | |
| 85 | const std::string& getTransferEncoding() const; |
| 86 | /// Returns the transfer encoding used for this |
| 87 | /// message. |
| 88 | /// |
| 89 | /// Normally, this is the value of the Transfer-Encoding |
| 90 | /// header field. If no such field is present, |
| 91 | /// returns IDENTITY_TRANSFER_CODING. |
| 92 | |
| 93 | void setChunkedTransferEncoding(bool flag); |
| 94 | /// If flag is true, sets the Transfer-Encoding header to |
| 95 | /// chunked. Otherwise, removes the Transfer-Encoding |
| 96 | /// header. |
| 97 | |
| 98 | bool getChunkedTransferEncoding() const; |
| 99 | /// Returns true if the Transfer-Encoding header is set |
| 100 | /// and its value is chunked. |
| 101 | |
| 102 | void setContentType(const std::string& mediaType); |
| 103 | /// Sets the content type for this message. |
| 104 | /// |
| 105 | /// Specify NO_CONTENT_TYPE to remove the |
| 106 | /// Content-Type header. |
| 107 | |
| 108 | void setContentType(const MediaType& mediaType); |
| 109 | /// Sets the content type for this message. |
| 110 | |
| 111 | const std::string& getContentType() const; |
| 112 | /// Returns the content type for this message. |
| 113 | /// |
| 114 | /// If no Content-Type header is present, |
| 115 | /// returns UNKNOWN_CONTENT_TYPE. |
| 116 | |
| 117 | void setKeepAlive(bool keepAlive); |
| 118 | /// Sets the value of the Connection header field. |
| 119 | /// |
| 120 | /// The value is set to "Keep-Alive" if keepAlive is |
| 121 | /// true, or to "Close" otherwise. |
| 122 | |
| 123 | bool getKeepAlive() const; |
| 124 | /// Returns true if |
| 125 | /// * the message has a Connection header field and its value is "Keep-Alive" |
| 126 | /// * the message is a HTTP/1.1 message and not Connection header is set |
| 127 | /// Returns false otherwise. |
| 128 | |
| 129 | static const std::string HTTP_1_0; |
| 130 | static const std::string HTTP_1_1; |
| 131 | |
| 132 | static const std::string IDENTITY_TRANSFER_ENCODING; |
| 133 | static const std::string CHUNKED_TRANSFER_ENCODING; |
| 134 | |
| 135 | static const int UNKNOWN_CONTENT_LENGTH; |
| 136 | static const std::string UNKNOWN_CONTENT_TYPE; |
| 137 | |
| 138 | static const std::string CONTENT_LENGTH; |
| 139 | static const std::string CONTENT_TYPE; |
| 140 | static const std::string TRANSFER_ENCODING; |
| 141 | static const std::string CONNECTION; |
| 142 | |
| 143 | static const std::string CONNECTION_KEEP_ALIVE; |
| 144 | static const std::string CONNECTION_CLOSE; |
| 145 | |
| 146 | static const std::string EMPTY; |
| 147 | |
| 148 | protected: |
| 149 | HTTPMessage(); |
| 150 | /// Creates the HTTPMessage with version HTTP/1.0. |
| 151 | |
| 152 | HTTPMessage(const std::string& version); |
| 153 | /// Creates the HTTPMessage and sets |
| 154 | /// the version. |
| 155 | |
| 156 | virtual ~HTTPMessage(); |
| 157 | /// Destroys the HTTPMessage. |
| 158 | |
| 159 | private: |
| 160 | HTTPMessage(const HTTPMessage&); |
| 161 | HTTPMessage& operator = (const HTTPMessage&); |
| 162 | |
| 163 | std::string _version; |
| 164 | }; |
| 165 | |
| 166 | |
| 167 | // |
| 168 | // inlines |
| 169 | // |
| 170 | inline const std::string& HTTPMessage::getVersion() const |
| 171 | { |
| 172 | return _version; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | inline bool HTTPMessage::hasContentLength() const |
| 177 | { |
| 178 | return has(CONTENT_LENGTH); |
| 179 | } |
| 180 | |
| 181 | |
| 182 | } } // namespace Poco::Net |
| 183 | |
| 184 | |
| 185 | #endif // Net_HTTPMessage_INCLUDED |
| 186 | |