Skip to content

Commit 0fdf66a

Browse files
committed
c
1 parent 728bf9a commit 0fdf66a

File tree

6 files changed

+648
-0
lines changed

6 files changed

+648
-0
lines changed

parsers/c/main.c

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
#include <unistd.h>
3+
#include <stdio.h>
4+
#include <string.h>
5+
#include <sys/types.h>
6+
#include <sys/socket.h>
7+
#include "request.h"
8+
#include "response.h"
9+
10+
11+
int main(int argc,char** argv)
12+
{
13+
if(argc != 3){
14+
perror("Invalid number of arguments\n");
15+
return EXIT_FAILURE;
16+
}
17+
int size = atoi(argv[2]);
18+
char* buffer = (char*)calloc(size+1,sizeof(char));
19+
size_t output_size = 0;
20+
if(read(0,buffer,size) == -1){
21+
perror("recv had an error");
22+
return EXIT_FAILURE;
23+
}
24+
25+
if(strcmp(argv[1],"response") == 0 ){
26+
struct ewp_response* resp = (struct ewp_response*)malloc(sizeof(struct ewp_response));
27+
if(ewp_response_parse(buffer,resp) != 0){
28+
write(2,"Parse failed\n",13);
29+
return EXIT_FAILURE;
30+
}
31+
char* out = ewp_response_marshal(resp,&output_size);
32+
write(1,out,output_size);
33+
}else if (strcmp(argv[1],"request") == 0){
34+
struct ewp_request* req = (struct ewp_request*)malloc(sizeof(struct ewp_request));
35+
if(ewp_request_parse(buffer,req) != 0){
36+
write(2,"Parse failed\n",13);
37+
return EXIT_FAILURE;
38+
}
39+
char* out = ewp_request_marshal(req,&output_size);
40+
write(1,out,output_size);
41+
}else{
42+
perror("Invalid first argument\n");
43+
return EXIT_FAILURE;
44+
}
45+
46+
47+
return EXIT_SUCCESS;
48+
}

parsers/c/makefile

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
CC=gcc
2+
3+
INCLUDE=-Ilib/
4+
VERSION=c11
5+
CFLAGS=-g -std=$(VERSION) -Wno-format -Wall
6+
7+
FILES=main.c
8+
9+
10+
.PHONY: clean fclean re all
11+
12+
all: $(OBJECTS) test
13+
14+
test:
15+
$(CC) $(CFLAGS) $(FILES) -o test
16+
17+
clean:
18+
rm -f $(OBJECTS) $(TEST_OBJ)
19+
20+
fclean:
21+
rm -f test
22+
23+
re: fclean all

parsers/c/request.h

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#ifndef HOBBIT_REQUEST_H
2+
#define HOBBIT_REQUEST_H
3+
/**
4+
* POC DO NOT USE IN PRODUCTION
5+
* UNSAFE UNSAFE UNSAFE
6+
*/
7+
#include <stdio.h>
8+
#include <string.h>
9+
#include "util.h"
10+
11+
struct ewp_request {
12+
char* proto;
13+
char* version;
14+
char* command;
15+
char* compression;
16+
vector response_compression;
17+
unsigned int head_only_indicator : 1;
18+
size_t header_len;
19+
size_t body_len;
20+
char* header;
21+
char* body;
22+
};
23+
24+
int ewp_request_parse(char* in,struct ewp_request* req)
25+
{
26+
int index = index_of_char(in,'\n');
27+
char* request_line = substring(in,0,index);
28+
29+
vector request = explode(" ",request_line);
30+
//printf("%d\n",vector_length(request));
31+
if(vector_length(request) < 7){
32+
vector_free(request);
33+
return 1;
34+
//Not enough parameters
35+
}
36+
37+
char* tmp = (char*)vector_get(request,0);
38+
req->proto = (char*)calloc(strlen(tmp)+1,sizeof(char));
39+
strcpy(req->proto,tmp);
40+
41+
tmp = (char*)vector_get(request,1);
42+
req->version = (char*)calloc(strlen(tmp)+1,sizeof(char));
43+
strcpy(req->version,tmp);
44+
45+
tmp = (char*)vector_get(request,2);
46+
req->command = (char*)calloc(strlen(tmp)+1,sizeof(char));
47+
strcpy(req->command,tmp);
48+
49+
tmp = (char*)vector_get(request,3);
50+
req->compression = (char*)calloc(strlen(tmp)+1,sizeof(char));
51+
strcpy(req->compression,tmp);
52+
53+
req->response_compression = explode(",",(char*)vector_get(request,4));
54+
req->head_only_indicator = (vector_length(request) == 8 && ((char*)vector_get(request,7))[0] == 'H');
55+
if(index != -1){
56+
char* request_body = substring(in,index+1,strlen(in));
57+
req->header_len = atoi((char*)vector_get(request,5));
58+
req->body_len = atoi((char*)vector_get(request,6));
59+
req->header = substring(request_body,0,req->header_len);
60+
req->body = substring(request_body,req->header_len,req->body_len);
61+
free(request_body);
62+
}
63+
vector_free(request);
64+
return 0;
65+
66+
}
67+
68+
char* ewp_request_marshal(struct ewp_request* req,size_t* size)
69+
{
70+
71+
char* tmp;
72+
char* out = req->proto;
73+
*size += strlen(out);
74+
tmp = strappend(' ',req->version);
75+
*size += strlen(tmp);
76+
out = concat(out,tmp, SECOND);
77+
tmp = strappend(' ',req->command);
78+
*size += strlen(tmp);
79+
out = concat(out,tmp,FIRST | SECOND);
80+
tmp = strappend(' ',req->compression);
81+
*size += strlen(tmp);
82+
out = concat(out,tmp,FIRST | SECOND);
83+
84+
size_t length = vector_length(req->response_compression);
85+
for (size_t i = 0; i < length; i++){
86+
if(i != 0){
87+
tmp = strappend(',',(char*)vector_get(req->response_compression,i));
88+
*size += strlen(tmp);
89+
out = concat(out,tmp,FIRST | SECOND);
90+
}else{
91+
*size += strlen((char*)vector_get(req->response_compression,i));
92+
out = concat(out,(char*)vector_get(req->response_compression,i),FIRST);
93+
}
94+
}
95+
tmp = strappend(' ',req->compression);
96+
*size += strlen(tmp);
97+
out = concat(out,tmp,FIRST | SECOND);
98+
99+
tmp = strappend(' ' ,ltoa(req->header_len));
100+
*size += strlen(tmp);
101+
out = concat(out,tmp,FIRST | SECOND);
102+
tmp = strappend(' ' ,ltoa(req->body_len));
103+
*size += strlen(tmp);
104+
out = concat(out,tmp,FIRST | SECOND);
105+
106+
if(req->head_only_indicator == 1){
107+
out = concat(out," H\n",FIRST);
108+
*size += 3;
109+
}else{
110+
tmp = concat(req->header,req->body,0);
111+
*size += strlen(tmp);
112+
out = concat(out,strappend('\n',tmp),FIRST|SECOND);
113+
free(tmp);
114+
}
115+
return out;
116+
}
117+
118+
119+
120+
121+
122+
#endif

parsers/c/response.h

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#ifndef HOBBIT_RESPONSE_H
2+
#define HOBBIT_RESPONSE_H
3+
/**
4+
* POC DO NOT USE IN PRODUCTION
5+
* UNSAFE UNSAFE UNSAFE
6+
*/
7+
#include <stdio.h>
8+
#include <string.h>
9+
#include "util.h"
10+
11+
12+
struct ewp_response{
13+
int response_status;
14+
char* compression;
15+
char* header;
16+
char* body;
17+
int header_length;
18+
int body_length;
19+
unsigned int has_body;
20+
};
21+
22+
23+
int ewp_response_parse(char* in,struct ewp_response* res)
24+
{
25+
int index = index_of_char(in,'\n');
26+
char* response_line = substring(in,0,index);
27+
28+
char* response_body = (char*)(in + index);
29+
30+
vector response = explode(" ",response_line);
31+
32+
33+
if(vector_length(response) < 3){
34+
vector_free(response);
35+
return 1;
36+
//Not enough parameters
37+
}
38+
39+
res->response_status = atoi((char*)vector_get(response,0));
40+
41+
res->compression = (char*)calloc(strlen((char*)vector_get(response,1))+1,sizeof(char));
42+
strcpy(res->compression,(char*)vector_get(response,1));
43+
44+
res->header_length = atoi((char*)vector_get(response,2));
45+
46+
res->header = substring(response_body,0,res->header_length);
47+
if(vector_length(response) == 4){
48+
res->body_length = atoi((char*)vector_get(response,3));
49+
res->body = substring(response_body, res->header_length, res->header_length+res->body_length);
50+
res->has_body = 1;
51+
}else{
52+
res->body_length = 0;
53+
res->has_body = 0;
54+
}
55+
vector_free(response);
56+
return 0;
57+
}
58+
59+
char* ewp_response_marshal(struct ewp_response* res,size_t* size)
60+
{
61+
char* tmp;
62+
char* intval;
63+
*size = 0;
64+
intval = ltoa(res->response_status);
65+
char* out = strappend(' ',ltoa(res->response_status));
66+
*size += strlen(out);
67+
tmp = strappend(' ',res->compression);
68+
*size += strlen(tmp);
69+
out = concat(out,tmp, FIRST|SECOND);
70+
intval = ltoa(res->header_length);
71+
tmp = strappend(' ',intval);
72+
*size += strlen(tmp);
73+
//free(intval);
74+
out = concat(out,tmp,FIRST | SECOND);
75+
if(res->has_body){
76+
intval = ltoa(res->header_length);
77+
tmp = strappend(' ',intval);
78+
*size += strlen(tmp);
79+
//free(intval);
80+
out = concat(out,tmp,FIRST | SECOND);
81+
}
82+
tmp = strappend('\n',out);
83+
*size += 1;
84+
free(out);
85+
out = tmp;
86+
*size += res->header_length + res->body_length;
87+
out = concat(out,res->header, FIRST);
88+
out = concat(out,res->body, FIRST);
89+
90+
return out;
91+
}
92+
93+
94+
95+
96+
97+
98+
#endif

0 commit comments

Comments
 (0)