Repository navigation
Expand file tree
/
Copy pathHTTPRequest.cpp
More file actions
83 lines (68 loc) · 1.68 KB
/
Copy pathHTTPRequest.cpp
File metadata and controls
83 lines (68 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* HTTPRequest.cpp
*
* Created on: Dec 13, 2017
* Author: frank
*/
#include "HTTPRequest.hpp"
namespace httpsserver {
HTTPRequest::HTTPRequest(ConnectionContext * con, HTTPHeaders * headers):
_con(con),
_headers(headers) {
HTTPHeader * contentLength = headers->get("Content-Length");
if (contentLength == NULL) {
_remainingContent = 0;
_contentLengthSet = false;
} else {
_remainingContent = parseInt(contentLength->_value);
_contentLengthSet = true;
}
}
HTTPRequest::~HTTPRequest() {
}
std::string HTTPRequest::getHeader(std::string name) {
HTTPHeader * h = _headers->get(name);
if (h != NULL) {
return h->_value;
} else {
return NULL;
}
}
size_t HTTPRequest::readBytes(byte * buffer, size_t length) {
// Limit reading to content length
if (_contentLengthSet && length > _remainingContent) {
length = _remainingContent;
}
if(length > 0 && SSL_want_read(_con->ssl())) {
size_t res = SSL_read(_con->ssl(), buffer, length);
if (res == 0) {
// Connection is closed.
// TODO: Is there _any_ way to differentiate between a closed connection and a
// connections that's still open but that won't provide any more bytes?
_con->signalClientClose();
return 0;
} else if (res == -1) {
// An error has occured, signal this
_con->signalRequestError();
return -1;
} else {
if (_contentLengthSet) {
_remainingContent -= res;
}
return res;
}
} else {
// No data to read, return 0.
return 0;
}
}
size_t HTTPRequest::readChars(char * buffer, size_t length) {
return 0;
}
size_t HTTPRequest::getContentLength() {
return _remainingContent;
}
bool HTTPRequest::requestComplete() {
return true;
}
} /* namespace httpsserver */