-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhttp-response.cc
148 lines (115 loc) · 3.25 KB
/
http-response.cc
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
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* @file Implementation for HttpResponse class
*
* Skeleton for UCLA CS118 Spring quarter class
*/
#include "http-response.h"
#include <string> // C++ STL string
#include <string.h> // helpers to copy C-style strings
#include "compat.h"
using namespace std;
// comment the following line to disable debug output
#define _DEBUG 1
#ifdef _DEBUG
#include <iostream>
#define TRACE(x) std::clog << x << endl;
#else
#endif // _DEBUG
#include <boost/lexical_cast.hpp>
HttpResponse::HttpResponse ()
{
}
const char*
HttpResponse::ParseResponse (const char *buffer, size_t size)
{
const char *curPos = buffer;
const char *endline = (const char *)memmem (curPos, size - (curPos-buffer), "\r\n", 2);
if (endline == 0)
{
throw ParseException ("HTTP response doesn't end with \\r\\n");
}
string responseLine (curPos, endline-curPos);
size_t posFirstSpace = responseLine.find (" ");
size_t posSecondSpace = responseLine.find (" ", posFirstSpace+1);
if (posFirstSpace == string::npos ||
posSecondSpace == string::npos)
{
throw ParseException ("Incorrectly formatted response");
}
// 1. HTTP version
if (responseLine.compare (0, 5, "HTTP/") != 0)
{
throw ParseException ("Incorrectly formatted HTTP response");
}
string version = responseLine.substr (5, posFirstSpace - 5);
// TRACE (version);
SetVersion (version);
// 2. Response code
string code = responseLine.substr (posFirstSpace + 1, posSecondSpace - posFirstSpace - 1);
// TRACE (code);
SetStatusCode (code);
string msg = responseLine.substr (posSecondSpace + 1, responseLine.size () - posSecondSpace - 1);
// TRACE (msg);
SetStatusMsg (msg);
curPos = endline + 2;
return ParseHeaders (curPos, size - (curPos-buffer));
}
size_t
HttpResponse::GetTotalLength () const
{
size_t len = 0;
len += 5; // 'HTTP/'
len += m_version.size (); // '1.0'
len += 1 + m_statusCode.size (); // ' code'
len += 1 + m_statusMsg.size (); // ' <msg>'
len += 2; // '\r\n'
len += HttpHeaders::GetTotalLength ();
len += 2; // '\r\n'
return len;
}
char*
HttpResponse::FormatResponse (char *buffer) const
{
char *bufLastPos = buffer;
bufLastPos = stpncpy (bufLastPos, "HTTP/", 5);
bufLastPos = stpncpy (bufLastPos, m_version.c_str (), m_version.size ());
bufLastPos = stpncpy (bufLastPos, " ", 1);
bufLastPos = stpncpy (bufLastPos, m_statusCode.c_str (), m_statusCode.size ());
bufLastPos = stpncpy (bufLastPos, " ", 1);
bufLastPos = stpncpy (bufLastPos, m_statusMsg.c_str (), m_statusMsg.size ());
bufLastPos = stpncpy (bufLastPos, "\r\n", 2);
bufLastPos = HttpHeaders::FormatHeaders (bufLastPos);
bufLastPos = stpncpy (bufLastPos, "\r\n", 2);
return bufLastPos;
}
const std::string &
HttpResponse::GetVersion () const
{
return m_version;
}
void
HttpResponse::SetVersion (const std::string &version)
{
m_version = version;
}
const std::string &
HttpResponse::GetStatusCode () const
{
return m_statusCode;
}
void
HttpResponse::SetStatusCode (const std::string &code)
{
m_statusCode = code;
}
const std::string &
HttpResponse::GetStatusMsg () const
{
return m_statusMsg;
}
void
HttpResponse::SetStatusMsg (const std::string &msg)
{
m_statusMsg = msg;
}