Skip to content

Commit bd184f1

Browse files
author
Chris Love
committed
Add operator<<() to http_request and http_response
1 parent e4b44dd commit bd184f1

File tree

7 files changed

+149
-11
lines changed

7 files changed

+149
-11
lines changed

examples/service.cpp

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
This file is part of libhttpserver
3-
Copyright (C) 2014 Chris Love
3+
Copyright (C) 2014 Sebastiano Merlino
44
55
This library is free software; you can redistribute it and/or
66
modify it under the terms of the GNU Lesser General Public
@@ -25,6 +25,8 @@
2525

2626
using namespace httpserver;
2727

28+
bool verbose=false;
29+
2830
class service_resource: public http_resource<service_resource> {
2931
public:
3032
service_resource();
@@ -63,7 +65,11 @@ service_resource::render_GET(const http_request &req, http_response** res)
6365
{
6466
std::cout << "service_resource::render_GET()" << std::endl;
6567

66-
*res = new http_response(http_response_builder("GET response", 200).string_response());
68+
if (verbose) std::cout << req;
69+
70+
*res = new http_response(http_response_builder("GET response", 200).string_response());
71+
72+
if (verbose) std::cout << **res;
6773
}
6874

6975

@@ -72,7 +78,11 @@ service_resource::render_PUT(const http_request &req, http_response** res)
7278
{
7379
std::cout << "service_resource::render_PUT()" << std::endl;
7480

75-
*res = new http_response(http_response_builder("PUT response", 200).string_response());
81+
if (verbose) std::cout << req;
82+
83+
*res = new http_response(http_response_builder("PUT response", 200).string_response());
84+
85+
if (verbose) std::cout << **res;
7686
}
7787

7888

@@ -81,14 +91,22 @@ service_resource::render_POST(const http_request &req, http_response** res)
8191
{
8292
std::cout << "service_resource::render_POST()" << std::endl;
8393

84-
*res = new http_response(http_response_builder("POST response", 200).string_response());
94+
if (verbose) std::cout << req;
95+
96+
*res = new http_response(http_response_builder("POST response", 200).string_response());
97+
98+
if (verbose) std::cout << **res;
8599
}
86100
void
87101
service_resource::render(const http_request &req, http_response** res)
88102
{
89103
std::cout << "service_resource::render()" << std::endl;
90104

91-
*res = new http_response(http_response_builder("generic response", 200).string_response());
105+
if (verbose) std::cout << req;
106+
107+
*res = new http_response(http_response_builder("generic response", 200).string_response());
108+
109+
if (verbose) std::cout << **res;
92110
}
93111

94112

@@ -97,31 +115,53 @@ service_resource::render_HEAD(const http_request &req, http_response** res)
97115
{
98116
std::cout << "service_resource::render_HEAD()" << std::endl;
99117

100-
*res = new http_response(http_response_builder("HEAD response", 200).string_response());
118+
if (verbose) std::cout << req;
119+
120+
*res = new http_response(http_response_builder("HEAD response", 200).string_response());
121+
122+
if (verbose) std::cout << **res;
101123
}
102124

103125
void
104126
service_resource::render_OPTIONS(const http_request &req, http_response** res)
105127
{
106128
std::cout << "service_resource::render_OPTIONS()" << std::endl;
107129

108-
*res = new http_response(http_response_builder("OPTIONS response", 200).string_response());
130+
if (verbose) std::cout << req;
131+
132+
*res = new http_response(http_response_builder("OPTIONS response", 200).string_response());
133+
134+
if (verbose) std::cout << **res;
109135
}
110136

111137
void
112138
service_resource::render_CONNECT(const http_request &req, http_response** res)
113139
{
114140
std::cout << "service_resource::render_CONNECT()" << std::endl;
115141

116-
*res = new http_response(http_response_builder("CONNECT response", 200).string_response());
142+
if (verbose) std::cout << req;
143+
144+
*res = new http_response(http_response_builder("CONNECT response", 200).string_response());
145+
146+
if (verbose) std::cout << **res;
117147
}
118148

119149
void
120150
service_resource::render_DELETE(const http_request &req, http_response** res)
121151
{
122152
std::cout << "service_resource::render_DELETE()" << std::endl;
123153

124-
*res = new http_response(http_response_builder("DELETE response", 200).string_response());
154+
if (verbose) std::cout << req;
155+
156+
*res = new http_response(http_response_builder("DELETE response", 200).string_response());
157+
158+
if (verbose) std::cout << **res;
159+
}
160+
161+
void usage()
162+
{
163+
std::cout << "Usage:" << std::endl
164+
<< "service [-p <port>][-s [-k <keyFileName>][-c <certFileName>]][-v]" << std::endl;
125165
}
126166

127167
int main(int argc, char **argv)
@@ -132,7 +172,7 @@ int main(int argc, char **argv)
132172
const char *cert="cert.pem";
133173
bool secure=false;
134174

135-
while ((c = getopt(argc,argv,"p:k:c:s")) != EOF) {
175+
while ((c = getopt(argc,argv,"p:k:c:sv?")) != EOF) {
136176
switch (c) {
137177
case 'p':
138178
port=strtoul(optarg,NULL,10);
@@ -145,8 +185,13 @@ int main(int argc, char **argv)
145185
break;
146186
case 's':
147187
secure=true;
188+
break;
189+
case 'v':
190+
verbose=true;
148191
break;
149192
default:
193+
usage();
194+
exit(1);
150195
break;
151196
}
152197
}

src/http_request.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "http_utils.hpp"
2323
#include "http_request.hpp"
2424
#include "string_utilities.hpp"
25+
#include <iostream>
2526

2627
using namespace std;
2728

@@ -85,4 +86,21 @@ size_t http_request::get_args(std::map<std::string, std::string, arg_comparator>
8586
return result.size();
8687
}
8788

88-
};
89+
std::ostream &operator<< (std::ostream &os, const http_request &r)
90+
{
91+
os << r.method << " Request [user:\"" << r.user << "\" pass:\"" << r.pass << "\"] path:\""
92+
<< r.path << "\"" << std::endl;
93+
94+
http::dump_header_map(os,"Headers",r.headers);
95+
http::dump_header_map(os,"Footers",r.footers);
96+
http::dump_header_map(os,"Cookies",r.cookies);
97+
http::dump_arg_map(os,"Query Args",r.args);
98+
99+
os << " Version [ " << r.version << " ] Requestor [ " << r.requestor
100+
<< " ] Port [ " << r.requestor_port << " ]" << std::endl;
101+
102+
return os;
103+
}
104+
105+
106+
}

src/http_response.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,16 @@ void http_response::get_raw_response_lp_send(
289289
ws->send_message_to_topic(send_topic, content);
290290
}
291291

292+
std::ostream &operator<< (std::ostream &os, const http_response &r)
293+
{
294+
os << "Response [response_code:" << r.response_code << "]" << std::endl;
295+
296+
http::dump_header_map(os,"Headers",r.headers);
297+
http::dump_header_map(os,"Footers",r.footers);
298+
http::dump_header_map(os,"Cookies",r.cookies);
299+
300+
return os;
301+
}
302+
303+
292304
};

src/http_utils.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <sstream>
3434
#include <iomanip>
3535
#include <fstream>
36+
#include <iostream>
3637
#include "string_utilities.hpp"
3738
#include "http_utils.hpp"
3839

@@ -541,5 +542,36 @@ char* load_file (const char *filename)
541542
return content;
542543
}
543544

545+
void dump_header_map(std::ostream &os, const std::string &prefix,
546+
const std::map<std::string,std::string,header_comparator> &map)
547+
{
548+
std::map<std::string,std::string,header_comparator>::const_iterator it = map.begin();
549+
std::map<std::string,std::string,header_comparator>::const_iterator end = map.end();
550+
551+
if (map.size()) {
552+
os << " " << prefix << " [";
553+
for (; it != end; ++it) {
554+
os << (*it).first << ":\"" << (*it).second << "\" ";
555+
}
556+
os << "]" << std::endl;
557+
}
558+
}
559+
560+
void dump_arg_map(std::ostream &os, const std::string &prefix,
561+
const std::map<std::string,std::string,arg_comparator> &map)
562+
{
563+
std::map<std::string,std::string,arg_comparator>::const_iterator it = map.begin();
564+
std::map<std::string,std::string,arg_comparator>::const_iterator end = map.end();
565+
566+
if (map.size()) {
567+
os << " " << prefix << " [";
568+
for (; it != end; ++it) {
569+
os << (*it).first << ":\"" << (*it).second << "\" ";
570+
}
571+
os << "]" << std::endl;
572+
}
573+
}
574+
575+
544576
};
545577
};

src/httpserver/http_request.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <vector>
3030
#include <string>
3131
#include <utility>
32+
#include <iosfwd>
3233

3334
struct MHD_Connection;
3435

@@ -355,6 +356,8 @@ class http_request
355356
const std::string& password,
356357
int nonce_timeout, bool& reload_nonce
357358
) const;
359+
360+
friend std::ostream &operator<< (std::ostream &os, const http_request &r);
358361
private:
359362
/**
360363
* Default constructor of the class. It is a specific responsibility of apis to initialize this type of objects.
@@ -585,5 +588,7 @@ class http_request
585588
friend class webserver;
586589
};
587590

591+
std::ostream &operator<< (std::ostream &os, const http_request &r);
592+
588593
};
589594
#endif

src/httpserver/http_response.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <map>
2828
#include <utility>
2929
#include <string>
30+
#include <iosfwd>
3031

3132
#include "httpserver/details/http_resource_mirror.hpp"
3233

@@ -69,6 +70,7 @@ typedef ssize_t(*cycle_callback_ptr)(const std::string&);
6970
class http_response
7071
{
7172
public:
73+
7274
http_response(const http_response_builder& builder);
7375

7476
/**
@@ -296,11 +298,14 @@ class http_response
296298
friend class http_response_builder;
297299
friend void clone_response(const http_response& hr, http_response** dhr);
298300
friend ssize_t details::cb(void* cls, uint64_t pos, char* buf, size_t max);
301+
friend std::ostream &operator<< (std::ostream &os, const http_response &r);
299302
private:
300303
http_response& operator=(const http_response& b);
301304

302305
static ssize_t data_generator (void* cls, uint64_t pos, char* buf, size_t max);
303306
};
304307

308+
std::ostream &operator<< (std::ostream &os, const http_response &r);
309+
305310
};
306311
#endif

src/httpserver/http_utils.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
#include <string>
3030
#include <cctype>
3131
#include <vector>
32+
#include <map>
3233
#include <algorithm>
3334
#include <exception>
35+
#include <iosfwd>
3436
#ifdef HAVE_GNUTLS
3537
#include <gnutls/gnutls.h>
3638
#endif
@@ -327,6 +329,25 @@ std::string get_ip_str_new(const struct sockaddr* sa,
327329
* @return short representing the port
328330
**/
329331
short get_port(const struct sockaddr* sa);
332+
333+
/**
334+
* Method to output the contents of a headers map to a std::ostream
335+
* @param os The ostream
336+
* @param prefix Prefix to identify the map
337+
* @param map
338+
**/
339+
void dump_header_map(std::ostream &os, const std::string &prefix,
340+
const std::map<std::string,std::string,header_comparator> &map);
341+
342+
/**
343+
* Method to output the contents of an arguments map to a std::ostream
344+
* @param os The ostream
345+
* @param prefix Prefix to identify the map
346+
* @param map
347+
**/
348+
void dump_arg_map(std::ostream &os, const std::string &prefix,
349+
const std::map<std::string,std::string,arg_comparator> &map);
350+
330351
/**
331352
* Process escape sequences ('+'=space, %HH) Updates val in place; the
332353
* result should be UTF-8 encoded and cannot be larger than the input.

0 commit comments

Comments
 (0)