-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTP_Message.h
More file actions
116 lines (96 loc) · 3.77 KB
/
HTTP_Message.h
File metadata and controls
116 lines (96 loc) · 3.77 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
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
https://powcoder.com
代写代考加微信 powcoder
Assignment Project Exam Help
Add WeChat powcoder
// Modified from CSE422 FS09
// Modified from CSE422 FS12
// Modified again for CSE422 FS20
#ifndef _HTTP_MESSAGE_H_
#define _HTTP_MESSAGE_H_
#include <map>
#include <string>
#include <vector>
namespace {
// The exact string of characters used to represent HTTP line endings.
const std::string line_ending = "\r\n";
// The character used to separate the name of a header from its value.
const char header_delimiter = ':';
}
/**
* Structure used to associate header names and values, whenever necessary.
*/
struct HTTP_Header_Field {
std::string name;
std::string value;
};
/**
* Base class for HTTP requests and responses. Defines the
* methods for accessing the various headers on a request/response. Also
* defines some internal things that are shared by the request/response classes.
*/
class HTTP_Message {
private:
std::map<std::string, std::string> headers;
protected:
HTTP_Message();
bool parse_fields(const char* data, unsigned length);
virtual void print(std::string& output_string) const;
virtual void print(char* output_buffer, unsigned buffer_length) const;
virtual void copy_if_room(char*& output_buffer, const char* data_string,
unsigned& remaining_length) const;
const char* find_next_line(const char* data, unsigned length) const;
public:
virtual ~HTTP_Message();
/**
* Indicates how many headers this message has.
*
* @return the number of headers stored in the message
*/
unsigned get_num_header_fields() const;
/**
* Copies all of the message's headers into the given vector. Use this
* if you need to iterate through the headers. If you know the name of
* the header you want, get_header_value() is far more useful.
*
* @param[out] out_set Will be set to a collection of
* HTTP_Header_Fields representing all of the headers stored in
* the message.
*/
void get_header_set(std::vector<HTTP_Header_Field>& out_set) const;
/**
* Retrieves the value of the header with the given name.
*
* @param[in] name The name of the header to look up.
* @param[out] out_value Will be set to that header's value, if it is
* found. If no header with that name is found, value will be
* undefined.
* @return True if the requested header was found in the message (in
* which case, out_value is valid); false if it was not found.
*/
bool get_header_value(const std::string& name,
std::string& out_value) const;
/**
* Removes the corresponding header field from the request, if it exists.
*
* @param name The name of the field to remove
*/
void remove_header_field(const std::string& name);
/**
* Updates the message to have the given header field. Overwrites the
* old value of the specified header if the message already had it.
*
* @param field The name/value of the header to set.
*/
void set_header_field(const HTTP_Header_Field& field);
/**
* Updates the given header field in the message. If the header is not
* already present, it will be added to the message. If the header
* *is* already present, its previous value will be overwritten.
*
* @param name The name of the header to set.
* @param value The new value to set.
*/
void set_header_field(const std::string& name,
const std::string& value);
};
#endif //ndef _HTTP_MESSAGE_H_