Skip to content

Commit 207eaa0

Browse files
committed
Move all parsers to 0.2
1 parent 5bb5ada commit 207eaa0

21 files changed

+66
-447
lines changed

parsers/c/main.c

+1-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <sys/types.h>
66
#include <sys/socket.h>
77
#include "request.h"
8-
#include "response.h"
98

109

1110
int main(int argc,char** argv)
@@ -22,15 +21,7 @@ int main(int argc,char** argv)
2221
return EXIT_FAILURE;
2322
}
2423

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){
24+
if (strcmp(argv[1],"request") == 0){
3425
struct ewp_request* req = (struct ewp_request*)malloc(sizeof(struct ewp_request));
3526
if(ewp_request_parse(buffer,req) != 0){
3627
write(2,"Parse failed\n",13);

parsers/c/request.h

+10-47
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ struct ewp_request {
1212
char* proto;
1313
char* version;
1414
char* command;
15-
char* compression;
16-
vector response_compression;
17-
unsigned int head_only_indicator : 1;
1815
size_t header_len;
1916
size_t body_len;
2017
char* header;
@@ -46,16 +43,10 @@ int ewp_request_parse(char* in,struct ewp_request* req)
4643
req->command = (char*)calloc(strlen(tmp)+1,sizeof(char));
4744
strcpy(req->command,tmp);
4845

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');
5546
if(index != -1){
5647
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));
48+
req->header_len = atoi((char*)vector_get(request,3));
49+
req->body_len = atoi((char*)vector_get(request,4));
5950
req->header = substring(request_body,0,req->header_len);
6051
req->body = substring(request_body,req->header_len,req->body_len);
6152
free(request_body);
@@ -70,36 +61,13 @@ char* ewp_request_marshal(struct ewp_request* req,size_t* size)
7061

7162
char* tmp;
7263
char* out =strappend(' ', req->proto);
73-
*size += strlen(out);
64+
*size += strlen(out);
7465
tmp = strappend(' ',req->version);
7566
*size += strlen(tmp);
7667
out = concat(out,tmp, SECOND);
7768
tmp = strappend(' ',req->command);
7869
*size += strlen(tmp);
7970
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-
tmp = (char*)vector_get(req->response_compression,i);
92-
*size += strlen(tmp);
93-
out = concat(out,tmp,FIRST);
94-
}
95-
96-
if( i == length - 1){
97-
tmp = strappend(' ',out);
98-
free(out);
99-
out = tmp;
100-
*size += 1;
101-
}
102-
}
10371

10472
tmp = strappend(' ' ,ltoa(req->header_len));
10573
*size += strlen(tmp);
@@ -109,21 +77,16 @@ char* ewp_request_marshal(struct ewp_request* req,size_t* size)
10977
*size += strlen(tmp);
11078
out = concat(out,tmp,FIRST);
11179

112-
if(req->head_only_indicator == 1){
113-
out = concat(out," H\n",FIRST);
114-
*size += 3;
115-
}else{
116-
tmp = strappend('\n',out);
117-
free(out);
118-
out = tmp;
119-
*size += 1;
80+
tmp = strappend('\n',out);
81+
free(out);
82+
out = tmp;
83+
*size += 1;
12084

121-
tmp = memsafe_concat(req->header,req->header_len,req->body,req->body_len,0);
85+
tmp = memsafe_concat(req->header,req->header_len,req->body,req->body_len,0);
12286

123-
out = memsafe_concat(out,*size,tmp,req->header_len + req->body_len,FIRST|SECOND);
124-
*size += req->header_len + req->body_len;
87+
out = memsafe_concat(out,*size,tmp,req->header_len + req->body_len,FIRST|SECOND);
88+
*size += req->header_len + req->body_len;
12589

126-
}
12790
return out;
12891
}
12992

parsers/c/response.h

-104
This file was deleted.

parsers/cpp/main.cpp

+1-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <algorithm>
55
#include <istream>
66
#include "request.h"
7-
#include "response.h"
87

98
using namespace std;
109

@@ -28,11 +27,9 @@ int main(int argc,char** argv)
2827
{
2928

3029
if(argc == 1){
31-
hobbit::ewp_request req(string("EWP 0.1 PING none none 0 5\n12345"));
32-
hobbit::ewp_response res(string("200 none 5 5\n1234512345"));
30+
hobbit::ewp_request req(string("EWP 0.2 PING 0 5\n12345"));
3331

3432
cout<<req.marshal()<<endl<<endl;
35-
cout<<res.marshal()<<endl<<endl;
3633
return EXIT_SUCCESS;
3734
}
3835

@@ -46,9 +43,6 @@ int main(int argc,char** argv)
4643
if(type == "request"){
4744
hobbit::ewp_request req(input);
4845
cout<<req.marshal();
49-
}else if(type == "response"){
50-
hobbit::ewp_response res(input);
51-
cout<<res.marshal();
5246
}else{
5347
cout<<"Unknown option"<<endl;
5448
}

parsers/cpp/request.h

+2-21
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ namespace hobbit{
1313
std::string proto;
1414
std::string version;
1515
std::string command;
16-
std::string compression;
17-
std::vector<std::string> response_compression;
18-
bool head_only_indicator;
1916
std::string header;
2017
std::string body;
2118

@@ -41,13 +38,10 @@ namespace hobbit{
4138
this->proto = request[0];
4239
this->version = request[1];
4340
this->command = request[2];
44-
this->compression = request[3];
45-
this->response_compression = explode(request[4],',');
46-
this->head_only_indicator = (request.size() == 8 && request[7] == "H");
4741
if(index != std::string::npos){
4842
const auto request_body = in.substr(index+1);
49-
this->header = request_body.substr(0,std::stoi(request[5]));
50-
this->body = request_body.substr(std::stoi(request[5]),std::stoi(request[6]));
43+
this->header = request_body.substr(0,std::stoi(request[3]));
44+
this->body = request_body.substr(std::stoi(request[3]),std::stoi(request[4]));
5145
}
5246

5347
}
@@ -57,21 +51,8 @@ namespace hobbit{
5751
std::string out = this->proto + " ";
5852
out += this->version + " ";
5953
out += this->command + " ";
60-
out += this->compression + " ";
61-
for (size_t i = 0; i < this->response_compression.size(); i++){
62-
if(i != 0){
63-
out += ",";
64-
}
65-
out += this->response_compression[i];
66-
}
6754
out += " " + std::to_string(this->header.size());
6855
out += " " + std::to_string(this->body.size());
69-
if(this->head_only_indicator){
70-
out += " H\n";
71-
}else{
72-
out += "\n";
73-
out += this->header + this->body;
74-
}
7556
return out;
7657
}
7758
};

parsers/cpp/response.h

-65
This file was deleted.

0 commit comments

Comments
 (0)