-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp1parser.cpp
64 lines (60 loc) · 1.97 KB
/
http1parser.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
53
54
55
56
57
58
59
60
61
62
63
#include "http1parser.h"
#include <iostream>
char *HTTP1_Parser::get_filename(const char *excutive_file)
{
int length = strlen(excutive_file);
static char buf[1024]={};
readlink( "/proc/self/exe", buf, sizeof(buf)-1 );
int buf_length = strlen(buf);
for(int i = 0; i <= length; i++){
buf[buf_length-i-1] = 0;
}
return buf;
}
std::string HTTP1_Parser::argv_path_analyzer(std::string request_path, const char *path, const char *executive_file) {
std::string path_string;
if(strcmp(path,"") == 0){
if (request_path == std::string("/")) {
path_string = std::string(HTTP1_Parser::get_filename(executive_file)) + std::string("/index.html");
}
else {
int judge = request_path.rfind(".",20);
if (judge >= 0 ) {
path_string = std::string(HTTP1_Parser::get_filename(executive_file)) + request_path;
} else {
path_string = std::string(HTTP1_Parser::get_filename(executive_file)) + request_path + std::string("/index.html");
}
}
}
else {
int judge = request_path.rfind(".",20);
if (judge >= 0 ) {
path_string = std::string(path) + request_path;
} else {
path_string = std::string(path) + request_path + std::string("/index.html");
}
}
return path_string;
}
std::string HTTP1_Parser::get_requestline_path(char _pbuf[BUF_SIZE]) {
int space_num = 0;
char path_buffer[32];
int current_point = 0;
std::string path_string;
for (int i = 0; i < BUF_SIZE; i++) {
if(space_num == 2){
path_buffer[current_point] = 0;
path_string = std::string(path_buffer);
return path_string;
}
if(space_num == 1){
if(_pbuf[i] != ' '){
path_buffer[current_point++] = _pbuf[i];
}
}
if(_pbuf[i] == ' '){
++space_num;
}
}
return "";
}