-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-request.h
171 lines (149 loc) · 3.63 KB
/
http-request.h
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* @file Header for HttpRequest class
*
* Skeleton for UCLA CS118 Spring quarter class
*/
#ifndef _HTTP_REQUEST_H_
#define _HTTP_REQUEST_H_
#include "http-headers.h"
/**
* @brief Class to parse/create HTTP requests
*
* HttpRequest objects are created from parsing a buffer containing a HTTP
* request. The request buffer consists of a request line followed by a number
* of headers. Request line fields such as method, protocol etc. are stored
* explicitly. Headers such as 'Content-Length' and their values are maintained
* in a linked list.
*
* Example:
* // command line parsing
* HttpRequest req;
* req.SetHost ("www.google.com");
* req.SetPort (80);
* req.SetMethod (HttpRequest::GET);
* req.SetPath ("/");
* req.SetVersion ("1.0");
* req.AddHeader ("Accept-Language", "en-US");
*
* size_t reqLen = req.GetTotalLength ();
* char *buf = new char [reqLen];
*
* req.FormatRequest (buf);
* cout << buf;
*
* delete [] buf;
*/
class HttpRequest : public HttpHeaders
{
public:
enum MethodEnum
{
GET = 0,
UNSUPPORTED = 1
};
/**
* @brief Default constructor
*
* Example:
* HttpRequest req;
* req.SetMethod (HttpRequest::GET);
* req.SetHost ("www.google.com");
* ...
*/
HttpRequest ();
/**
* @brief Parse HTTP header
*
* Example:
* HttpRequest req;
* const char *buf = "GET http://www.google.com:80/index.html/ HTTP/1.0\r\nContent-Length:"
* " 80\r\nIf-Modified-Since: Sat, 29 Oct 1994 19:43:31 GMT\r\n\r\n";
* req.Parse (buf);
*/
const char*
ParseRequest (const char *buffer, size_t size);
/**
* @brief Get total length of the HTTP header (buffer size necessary to hold formatted HTTP request)
*/
size_t
GetTotalLength () const;
/**
* @brief Format HTTP request
*
* Note that buffer size should be enough to hold the request (e.g., obtained from GetTotalLength () call). Otherwise, anything can happen.
*
* @param buffer [out] Buffer that will hold formatted request
* @returns Number of bytes actually written to the buffer
*/
char*
FormatRequest (char *buffer) const;
// Getters/Setters for HTTP request fields
/**
* @brief Get method of the HTTP request
*/
MethodEnum
GetMethod () const;
/**
* @brief Set method of the HTTP request
*/
void
SetMethod (MethodEnum method);
// /**
// * @brief Set method of the HTTP request
// */
// const std::string &
// GetProtocol () const;
// /**
// * @brief Set method of the HTTP request
// */
// void
// SetProtocol (const std::string &protocol);
/**
* @brief Get host of the HTTP request
*/
const std::string &
GetHost () const;
/**
* @brief Set host of the HTTP request
*/
void
SetHost (const std::string &host);
/**
* @brief Get port of the HTTP request
*/
unsigned short
GetPort () const;
/**
* @brief Set port of the HTTP request
*/
void
SetPort (unsigned short port);
/**
* @brief Get path of the HTTP request
*/
const std::string &
GetPath () const;
/**
* @brief Set path of the HTTP request
*/
void
SetPath (const std::string &path);
/**
* @brief Get version of the HTTP request
*/
const std::string &
GetVersion () const;
/**
* @brief Set version of the HTTP request
*/
void
SetVersion (const std::string &version);
private:
MethodEnum m_method;
std::string m_host;
unsigned short m_port;
std::string m_path;
std::string m_version;
};
#endif // _HTTP_REQUEST_H_