Skip to content

Commit

Permalink
mp => form; HttpMessage Get<> Set
Browse files Browse the repository at this point in the history
  • Loading branch information
ithewei committed Jan 19, 2020
1 parent d01215c commit 85cdfef
Show file tree
Hide file tree
Showing 8 changed files with 288 additions and 28 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ libhv:
$(CP) $(INSTALL_HEADERS) include/hv

install:
$(MKDIR) -p $(INSTALL_INCDIR)
$(MKDIR) $(INSTALL_INCDIR)
$(CP) include/hv/* $(INSTALL_INCDIR)
$(CP) lib/libhv.a lib/libhv.so $(INSTALL_LIBDIR)

Expand Down Expand Up @@ -51,6 +51,7 @@ else
endif

httpd: prepare
$(RM) examples/httpd/*.o
$(MAKEF) TARGET=$@ SRCDIRS=". base utils event http http/server examples/httpd"

curl: prepare
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,15 @@ bin/curl -v localhost:8080/downloads/
bin/curl -v localhost:8080/v1/api/hello
bin/curl -v localhost:8080/v1/api/echo -d "hello,world!"
bin/curl -v localhost:8080/v1/api/query?page_no=1&page_size=10
bin/curl -v localhost:8080/v1/api/json -H "Content-Type:application/json" -d '{"user":"admin","pswd":"123456"}'
bin/curl -v localhost:8080/v1/api/kv -H "Content-Type:application/x-www-form-urlencoded" -d 'user=admin&pswd=123456'
bin/curl -v localhost:8080/v1/api/mp -F "file=@LICENSE"
bin/curl -v localhost:8080/v1/api/json -H "Content-Type:application/json" -d '{"user":"admin","pswd":"123456"}'
bin/curl -v localhost:8080/v1/api/form -F "file=@LICENSE"
bin/curl -v localhost:8080/v1/api/test -H "Content-Type:application/x-www-form-urlencoded" -d
'bool=1&int=123&float=3.14&string=hello'
bin/curl -v localhost:8080/v1/api/test -H "Content-Type:application/json" -d
'{"bool":true,"int":123,"float":3.14,"string":"hello"}'
bin/curl -v localhost:8080/v1/api/test -F 'bool=1 int=123 float=3.14 string=hello'
# RESTful API: /group/:group_name/user/:user_id
bin/curl -v -X DELETE localhost:8080/v1/api/group/test/user/123
Expand Down
2 changes: 1 addition & 1 deletion etc/httpd.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ log_filesize = 64M

# worker_processes = 4
# auto = ncpu
worker_processes = 1
worker_processes = 4

# http server
ssl = off
Expand Down
10 changes: 5 additions & 5 deletions examples/curl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ static const char* help = R"(Options:
curl -v localhost:8080/v1/api/hello
curl -v localhost:8080/v1/api/query?page_no=1&page_size=10
curl -v localhost:8080/v1/api/echo -d 'hello,world!'
curl -v localhost:8080/v1/api/json -H "Content-Type:application/json" -d '{"user":"admin","pswd":"123456"}'
curl -v localhost:8080/v1/api/kv -H "Content-Type:application/x-www-form-urlencoded" -d 'user=admin&pswd=123456'
curl -v localhost:8080/v1/api/mp -F 'file=@filename'
curl -v localhost:8080/v1/api/json -H "Content-Type:application/json" -d '{"user":"admin","pswd":"123456"}'
curl -v localhost:8080/v1/api/form -F 'file=@filename'
)";

void print_usage() {
Expand Down Expand Up @@ -164,7 +164,7 @@ int main(int argc, char* argv[]) {
int value_len = 0;
state = s_key;
while (*p != '\0') {
if (*p == ';') {
if (*p == ' ') {
if (key_len && value_len) {
FormData data;
if (*value == '@') {
Expand All @@ -173,7 +173,7 @@ int main(int argc, char* argv[]) {
else {
data.content = std::string(value, value_len);
}
req.mp[std::string(key,key_len)] = data;
req.form[std::string(key,key_len)] = data;
key_len = value_len = 0;
}
state = s_key;
Expand All @@ -197,7 +197,7 @@ int main(int argc, char* argv[]) {
else {
data.content = std::string(value, value_len);
}
req.mp[std::string(key,key_len)] = data;
req.form[std::string(key,key_len)] = data;
}
}
}
Expand Down
53 changes: 41 additions & 12 deletions examples/httpd/http_api_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,24 @@
XXX("/hello", GET, http_api_hello) \
XXX("/query", GET, http_api_query) \
XXX("/echo", POST, http_api_echo) \
XXX("/json", POST, http_api_json) \
XXX("/mp", POST, http_api_mp) \
XXX("/kv", POST, http_api_kv) \
XXX("/json", POST, http_api_json) \
XXX("/form", POST, http_api_form) \
XXX("/grpc", POST, http_api_grpc) \
\
XXX("/test", POST, http_api_test) \
XXX("/group/:group_name/user/:user_id", DELETE, http_api_restful) \

inline void response_status(HttpResponse* res, int code, const char* message) {
res->Set("code", code);
res->Set("message", message);
}

inline int http_api_preprocessor(HttpRequest* req, HttpResponse* res) {
//printf("%s:%d\n", req->client_addr.ip.c_str(), req->client_addr.port);
//printf("%s\n", req->Dump(true, true).c_str());
req->ParseBody();
res->content_type = APPLICATION_JSON;
return 0;
}

Expand All @@ -44,30 +51,33 @@ inline int http_api_echo(HttpRequest* req, HttpResponse* res) {
return 0;
}

inline int http_api_json(HttpRequest* req, HttpResponse* res) {
if (req->content_type != APPLICATION_JSON) {
inline int http_api_kv(HttpRequest*req, HttpResponse* res) {
if (req->content_type != APPLICATION_URLENCODED) {
res->status_code = HTTP_STATUS_BAD_REQUEST;
return 0;
}
res->json = req->json;
res->content_type = APPLICATION_URLENCODED;
res->kv = req->kv;
return 0;
}

inline int http_api_mp(HttpRequest* req, HttpResponse* res) {
if (req->content_type != MULTIPART_FORM_DATA) {
inline int http_api_json(HttpRequest* req, HttpResponse* res) {
if (req->content_type != APPLICATION_JSON) {
res->status_code = HTTP_STATUS_BAD_REQUEST;
return 0;
}
res->mp = req->mp;
res->content_type = APPLICATION_JSON;
res->json = req->json;
return 0;
}

inline int http_api_kv(HttpRequest*req, HttpResponse* res) {
if (req->content_type != APPLICATION_URLENCODED) {
inline int http_api_form(HttpRequest* req, HttpResponse* res) {
if (req->content_type != MULTIPART_FORM_DATA) {
res->status_code = HTTP_STATUS_BAD_REQUEST;
return 0;
}
res->kv = req->kv;
res->content_type = MULTIPART_FORM_DATA;
res->form = req->form;
return 0;
}

Expand All @@ -78,14 +88,33 @@ inline int http_api_grpc(HttpRequest* req, HttpResponse* res) {
}
// parse protobuf: ParseFromString
// req->body;
// res->content_type = APPLICATION_GRPC;
// serailize protobuf: SerializeAsString
// res->body;
return 0;
}

inline int http_api_test(HttpRequest* req, HttpResponse* res) {
string str = req->GetValue("string");
int64_t n = req->Get<int64_t>("int");
double f = req->Get<double>("float");
bool b = req->Get<bool>("bool");

res->content_type = req->content_type;
res->Set("string", str);
res->Set("int", n);
res->Set("float", f);
res->Set("bool", b);
response_status(res, 0, "OK");
return 0;
}

inline int http_api_restful(HttpRequest*req, HttpResponse* res) {
// RESTful /:field/ => req->query_params
res->kv = req->query_params;
for (auto& param : req->query_params) {
res->Set(param.first.c_str(), param.second);
}
response_status(res, 0, "Operation completed.");
return 0;
}

Expand Down
4 changes: 2 additions & 2 deletions html/downloads/scripts/getting_started.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ bin/curl -v localhost:8080/downloads/
bin/curl -v localhost:8080/v1/api/hello
bin/curl -v localhost:8080/v1/api/echo -d "hello,world!"
bin/curl -v localhost:8080/v1/api/query?page_no=1&page_size=10
bin/curl -v localhost:8080/v1/api/json -H "Content-Type:application/json" -d '{"user":"admin","pswd":"123456"}'
bin/curl -v localhost:8080/v1/api/kv -H "Content-Type:application/x-www-form-urlencoded" -d 'user=admin&pswd=123456'
bin/curl -v localhost:8080/v1/api/mp -F "file=@LICENSE"
bin/curl -v localhost:8080/v1/api/json -H "Content-Type:application/json" -d '{"user":"admin","pswd":"123456"}'
bin/curl -v localhost:8080/v1/api/form -F "file=@LICENSE"
# RESTful API: /group/:group_name/user/:user_id
bin/curl -v -X DELETE localhost:8080/v1/api/group/test/user/123
Loading

0 comments on commit 85cdfef

Please sign in to comment.