-
Notifications
You must be signed in to change notification settings - Fork 2
/
http-headers.h
111 lines (94 loc) · 2.32 KB
/
http-headers.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
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* @file Header for HttpRequest class
*
* Skeleton for UCLA CS118 Winter'14 class
*/
#ifndef _HTTP_HEADERS_H_
#define _HTTP_HEADERS_H_
#include <string>
#include <list>
/**
* Exception that will be thrown when parsing cannot be performed
*/
class ParseException : public std::exception
{
public:
ParseException (const std::string &reason) : m_reason (reason) { }
virtual ~ParseException () throw () { }
virtual const char* what() const throw ()
{ return m_reason.c_str (); }
private:
std::string m_reason;
};
/**
* @brief Class to parse/create HTTP headers
*/
class HttpHeaders
{
public:
/**
* @brief Default constructor
*/
HttpHeaders ();
/**
* @brief Parse HTTP headers
*/
const char*
ParseHeaders (const char *buffer, size_t size);
/**
* @brief Get total length of the HTTP headers (buffer size necessary to hold formatted HTTP headers)
*/
size_t
GetTotalLength () const;
/**
* @brief Format HTTP headers
*
* Another note. 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*
FormatHeaders (char *buffer) const;
// Getters/Setters for HTTP request fields
/**
* @brief Add HTTP header
*/
void
AddHeader (const std::string &key, const std::string &value);
/**
* @brief Remove HTTP header
*/
void
RemoveHeader (const std::string &key);
/**
* @brief Modify HTTP header
*
* Note that if header is currently not present, it will be added
*/
void
ModifyHeader (const std::string &key, const std::string &value);
/**
* @brief Find value for the `key' http header
*
* If header doesn't exist, it the method will return a blank line
*/
std::string
FindHeader (const std::string &key);
private:
struct HttpHeader
{
HttpHeader (const std::string &key, const std::string &value)
: m_key (key), m_value (value) { }
bool
operator == (const std::string &key) const
{
return key == m_key;
}
std::string m_key;
std::string m_value;
};
std::list<HttpHeader> m_headers;
};
#endif // _HTTP_HEADERS_H_