-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttpparser.cpp
52 lines (41 loc) · 1.35 KB
/
httpparser.cpp
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
//
// Created by valdemar on 17.07.16.
//
#include "httpparser.h"
#include <cstring>
int parse_request(char *msg, http_response_msg &response, const char *webserv_root_dir) {
response.req_file = nullptr;
response.headers.clear();
char *pos = strtok(msg, " ");
std::string method = pos;
std::string filepath = webserv_root_dir;
filepath += strtok(nullptr, " ");
//Get params support
unsigned long idx = filepath.find('?');
if (idx != std::string::npos) {
filepath[idx] = '\0';
}
std::string http_ver = strtok(nullptr, "\n");
http_ver.pop_back();
if (http_ver.back() == '\r') {
http_ver.pop_back();
}
FILE *file = fopen(filepath.c_str(), "r");
response.req_file = file;
//printf("Info: Http=%s, Method=%s, URI=%s, File=%s\n",
// http_ver.c_str(), method.c_str(), filepath.c_str(),
// file ? "Exists" : "Not found");
response.status_line = http_ver;
response.status_line += " ";
if (file) {
response.status_line += "200 Ok";
fseek(file, 0, SEEK_END);
long byte_size = ftell(file);
response.headers.emplace_back("Content-Length: " + std::to_string(byte_size));
rewind(file);
} else {
response.status_line += "404 Not found";
response.headers.emplace_back("Content-Length: 0");
}
return 0;
}