|
| 1 | +/* |
| 2 | + * HTTPResponse.cpp |
| 3 | + * |
| 4 | + * Created on: Dec 13, 2017 |
| 5 | + * Author: frank |
| 6 | + */ |
| 7 | + |
| 8 | +#include "HTTPResponse.hpp" |
| 9 | + |
| 10 | +namespace httpsserver { |
| 11 | + |
| 12 | +HTTPResponse::HTTPResponse(ConnectionContext * con): |
| 13 | + _con(con) { |
| 14 | + |
| 15 | + // Default status code is 200 OK |
| 16 | + _statusCode = 200; |
| 17 | + _statusText = "OK"; |
| 18 | + _headerWritten = false; |
| 19 | +} |
| 20 | + |
| 21 | +HTTPResponse::~HTTPResponse() { |
| 22 | + |
| 23 | +} |
| 24 | + |
| 25 | +void HTTPResponse::setStatusCode(uint16_t statusCode) { |
| 26 | + _statusCode = statusCode; |
| 27 | +} |
| 28 | + |
| 29 | +void HTTPResponse::setStatusText(std::string statusText) { |
| 30 | + _statusText = statusText; |
| 31 | +} |
| 32 | + |
| 33 | +void HTTPResponse::setHeader(std::string name, std::string value) { |
| 34 | + _headers.set(new HTTPHeader(name, value)); |
| 35 | +} |
| 36 | + |
| 37 | +bool HTTPResponse::isHeaderWritten() { |
| 38 | + return _headerWritten; |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Writes a string to the response. May be called several times. |
| 43 | + */ |
| 44 | +void HTTPResponse::print(const std::string &str) { |
| 45 | + printHeader(); |
| 46 | + printInternal(str); |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Writes bytes to the response. May be called several times. |
| 51 | + */ |
| 52 | +void HTTPResponse::writeBytes(const void * data, int length) { |
| 53 | + printHeader(); |
| 54 | + writeBytesInternal(data, length); |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * If not already done, writes the header. |
| 59 | + */ |
| 60 | +void HTTPResponse::printHeader() { |
| 61 | + if (!_headerWritten) { |
| 62 | + // FIXME: The usage of ostringstream increses the binary size by 200kB. |
| 63 | + // std::to_string is only available for the 2011 standard, but maybe |
| 64 | + // one can change this to a int-string-conversion this is performed manually |
| 65 | + std::ostringstream statusLine; |
| 66 | + statusLine << "HTTP/1.1" << _statusCode << " " << _statusText << "\r\n"; |
| 67 | + printInternal(statusLine.str()); |
| 68 | + std::vector<HTTPHeader *> * headers = _headers.getAll(); |
| 69 | + for(std::vector<HTTPHeader*>::iterator header = headers->begin(); header != headers->end(); ++header) { |
| 70 | + printInternal((*header)->print()+"\r\n"); |
| 71 | + } |
| 72 | + printInternal("\r\n"); |
| 73 | + |
| 74 | + // TODO: If we wanted to use Connection: keep-alive, we need to know the content size now. |
| 75 | + |
| 76 | + _headerWritten=true; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +void HTTPResponse::printInternal(const std::string &str) { |
| 81 | + writeBytesInternal(str.c_str(), str.length()); |
| 82 | +} |
| 83 | + |
| 84 | +void HTTPResponse::writeBytesInternal(const void * data, int length) { |
| 85 | + SSL_write(_con->ssl(), data, length); |
| 86 | +} |
| 87 | + |
| 88 | +} /* namespace httpsserver */ |
0 commit comments