Skip to content
This repository was archived by the owner on Jun 12, 2018. It is now read-only.

Commit c58b7a7

Browse files
committed
Response stream now subclass std::ostream. Also some cleanup of default_resource example.
1 parent 4bc5078 commit c58b7a7

File tree

3 files changed

+88
-112
lines changed

3 files changed

+88
-112
lines changed

http_examples.cpp

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -84,51 +84,47 @@ int main() {
8484
server.default_resource["GET"]=[](HttpServer::Response& response, shared_ptr<HttpServer::Request> request) {
8585
boost::filesystem::path web_root_path("web");
8686
if(!boost::filesystem::exists(web_root_path))
87-
cerr << "Could not find web root." << endl;
87+
cerr << "Could not find web root." << endl;
8888
else {
89-
auto path=web_root_path;
90-
path+=request->path;
91-
if(boost::filesystem::exists(path)) {
92-
if(boost::filesystem::canonical(web_root_path)<=boost::filesystem::canonical(path)) {
93-
if(boost::filesystem::is_directory(path))
94-
path+="/index.html";
95-
if(boost::filesystem::exists(path) && boost::filesystem::is_regular_file(path)) {
96-
ifstream ifs;
97-
ifs.open(path.string(), ifstream::in | ios::binary);
98-
99-
if(ifs) {
100-
ifs.seekg(0, ios::end);
101-
size_t length=ifs.tellg();
102-
103-
ifs.seekg(0, ios::beg);
89+
auto path=web_root_path;
90+
path+=request->path;
91+
if(boost::filesystem::exists(path)) {
92+
if(boost::filesystem::canonical(web_root_path)<=boost::filesystem::canonical(path)) {
93+
if(boost::filesystem::is_directory(path))
94+
path+="/index.html";
95+
if(boost::filesystem::exists(path) && boost::filesystem::is_regular_file(path)) {
96+
ifstream ifs;
97+
ifs.open(path.string(), ifstream::in | ios::binary);
98+
99+
if(ifs) {
100+
ifs.seekg(0, ios::end);
101+
size_t length=ifs.tellg();
102+
103+
ifs.seekg(0, ios::beg);
104+
105+
response << "HTTP/1.1 200 OK\r\nContent-Length: " << length << "\r\n\r\n";
106+
107+
//read and send 128 KB at a time
108+
size_t buffer_size=131072;
109+
vector<char> buffer;
110+
buffer.reserve(buffer_size);
111+
size_t read_length;
112+
try {
113+
while((read_length=ifs.read(&buffer[0], buffer_size).gcount())>0) {
114+
response.write(&buffer[0], read_length);
115+
response.flush();
116+
}
117+
}
118+
catch(const exception &e) {
119+
cerr << "Connection interrupted, closing file" << endl;
120+
}
104121

105-
response << "HTTP/1.1 200 OK\r\nContent-Length: " << length << "\r\n\r\n";
106-
107-
//read and send 128 KB at a time if file-size>buffer_size
108-
size_t buffer_size=131072;
109-
if(length>buffer_size) {
110-
vector<char> buffer;
111-
buffer.reserve(buffer_size);
112-
size_t read_length;
113-
try {
114-
while((read_length=ifs.read(&buffer[0], buffer_size).gcount())>0) {
115-
response.stream.write(&buffer[0], read_length);
116-
response << HttpServer::flush;
117-
}
118-
}
119-
catch(const exception &e) {
120-
cerr << "Connection interrupted, closing file" << endl;
121-
}
122-
}
123-
else
124-
response << ifs.rdbuf();
125-
126-
ifs.close();
127-
return;
122+
ifs.close();
123+
return;
124+
}
125+
}
128126
}
129-
}
130127
}
131-
}
132128
}
133129
string content="Could not open path "+request->path;
134130
response << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << content.length() << "\r\n\r\n" << content;

https_examples.cpp

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -84,51 +84,47 @@ int main() {
8484
server.default_resource["GET"]=[](HttpsServer::Response& response, shared_ptr<HttpsServer::Request> request) {
8585
boost::filesystem::path web_root_path("web");
8686
if(!boost::filesystem::exists(web_root_path))
87-
cerr << "Could not find web root." << endl;
87+
cerr << "Could not find web root." << endl;
8888
else {
89-
auto path=web_root_path;
90-
path+=request->path;
91-
if(boost::filesystem::exists(path)) {
92-
if(boost::filesystem::canonical(web_root_path)<=boost::filesystem::canonical(path)) {
93-
if(boost::filesystem::is_directory(path))
94-
path+="/index.html";
95-
if(boost::filesystem::exists(path) && boost::filesystem::is_regular_file(path)) {
96-
ifstream ifs;
97-
ifs.open(path.string(), ifstream::in | ios::binary);
98-
99-
if(ifs) {
100-
ifs.seekg(0, ios::end);
101-
size_t length=ifs.tellg();
102-
103-
ifs.seekg(0, ios::beg);
89+
auto path=web_root_path;
90+
path+=request->path;
91+
if(boost::filesystem::exists(path)) {
92+
if(boost::filesystem::canonical(web_root_path)<=boost::filesystem::canonical(path)) {
93+
if(boost::filesystem::is_directory(path))
94+
path+="/index.html";
95+
if(boost::filesystem::exists(path) && boost::filesystem::is_regular_file(path)) {
96+
ifstream ifs;
97+
ifs.open(path.string(), ifstream::in | ios::binary);
98+
99+
if(ifs) {
100+
ifs.seekg(0, ios::end);
101+
size_t length=ifs.tellg();
102+
103+
ifs.seekg(0, ios::beg);
104+
105+
response << "HTTP/1.1 200 OK\r\nContent-Length: " << length << "\r\n\r\n";
106+
107+
//read and send 128 KB at a time
108+
size_t buffer_size=131072;
109+
vector<char> buffer;
110+
buffer.reserve(buffer_size);
111+
size_t read_length;
112+
try {
113+
while((read_length=ifs.read(&buffer[0], buffer_size).gcount())>0) {
114+
response.write(&buffer[0], read_length);
115+
response.flush();
116+
}
117+
}
118+
catch(const exception &e) {
119+
cerr << "Connection interrupted, closing file" << endl;
120+
}
104121

105-
response << "HTTP/1.1 200 OK\r\nContent-Length: " << length << "\r\n\r\n";
106-
107-
//read and send 128 KB at a time if file-size>buffer_size
108-
size_t buffer_size=131072;
109-
if(length>buffer_size) {
110-
vector<char> buffer;
111-
buffer.reserve(buffer_size);
112-
size_t read_length;
113-
try {
114-
while((read_length=ifs.read(&buffer[0], buffer_size).gcount())>0) {
115-
response.stream.write(&buffer[0], read_length);
116-
response << HttpsServer::flush;
117-
}
118-
}
119-
catch(const exception &e) {
120-
cerr << "Connection interrupted, closing file" << endl;
121-
}
122-
}
123-
else
124-
response << ifs.rdbuf();
125-
126-
ifs.close();
127-
return;
122+
ifs.close();
123+
return;
124+
}
125+
}
128126
}
129-
}
130127
}
131-
}
132128
}
133129
string content="Could not open path "+request->path;
134130
response << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << content.length() << "\r\n\r\n" << content;

server_http.hpp

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace SimpleWeb {
1414
template <class socket_type>
1515
class ServerBase {
1616
public:
17-
class Response {
17+
class Response : public std::ostream {
1818
friend class ServerBase<socket_type>;
1919
private:
2020
boost::asio::yield_context& yield;
@@ -24,37 +24,19 @@ namespace SimpleWeb {
2424
socket_type &socket;
2525

2626
Response(boost::asio::io_service& io_service, socket_type &socket, boost::asio::yield_context& yield):
27-
yield(yield), socket(socket), stream(&streambuf) {}
28-
27+
std::ostream(&streambuf), yield(yield), socket(socket) {}
28+
29+
public:
30+
size_t size() {
31+
return streambuf.size();
32+
}
2933
void flush() {
3034
boost::system::error_code ec;
3135
boost::asio::async_write(socket, streambuf, yield[ec]);
3236

3337
if(ec)
3438
throw std::runtime_error(ec.message());
3539
}
36-
37-
public:
38-
std::ostream stream;
39-
40-
size_t size() {
41-
return streambuf.size();
42-
}
43-
44-
template <class T>
45-
Response& operator<<(const T& t) {
46-
stream << t;
47-
return *this;
48-
}
49-
50-
Response& operator<<(std::ostream& (*manip)(std::ostream&)) {
51-
stream << manip;
52-
return *this;
53-
}
54-
55-
Response& operator<<(Response& (*manip)(Response&)) {
56-
return manip(*this);
57-
}
5840
};
5941

6042
static Response& flush(Response& r) {
@@ -307,11 +289,13 @@ namespace SimpleWeb {
307289
return;
308290
}
309291

310-
try {
311-
response.flush();
312-
}
313-
catch(const std::exception &e) {
314-
return;
292+
if(response.size()>0) {
293+
try {
294+
response.flush();
295+
}
296+
catch(const std::exception &e) {
297+
return;
298+
}
315299
}
316300
if(timeout_content>0)
317301
timer->cancel();

0 commit comments

Comments
 (0)