-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.cpp
More file actions
51 lines (40 loc) · 1.17 KB
/
request.cpp
File metadata and controls
51 lines (40 loc) · 1.17 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
#include "request.hpp"
#include "logger.hpp"
#include <unistd.h>
#include <sstream>
extern Log main_log;
Request::Request ( int file_descriptor )
: client_request_size ( 1024 )
{
/* It is nescessary to use a char* rather than a std::string since read
* takes mutable C-strings as arguments.
*/
char* client_request = new char [client_request_size+1];
ssize_t amount_read;
/* Get the request from the socket */
if ( ( amount_read = read ( file_descriptor,client_request,client_request_size ) ) == -1 )
{
// We enter this block if the read failed.
throw 500; // Internal Server Error
}
else
{
client_request[amount_read] = 0; // This ensures we have a valid C-string
}
/* Parse the request header */
std::stringstream request_stream ( client_request );
request_stream >> method;
request_stream >> URI;
request_stream >> http_version;
if ( request_stream.fail() )
{
main_log ( std::string ( "Received a bad request:\n" ) + client_request, NOTICE );
throw 400; // Bad Request
}
else
{
main_log ( "Method: " + method + "\nRequested URI: " + URI + "\nHTTP Version: " + http_version, DEBUG );
}
raw = client_request;
delete[] client_request;
}