OCT 重构DHCP HTTP服务消息框架
This commit is contained in:
parent
b8ece8e445
commit
c072c8bc08
|
@ -17,6 +17,7 @@ using namespace std;
|
|||
#include "opendhcpd.h"
|
||||
#include "s2j/cJSON.h"
|
||||
#include "haywire.h"
|
||||
#include "misc.h"
|
||||
#include <net/if_arp.h>
|
||||
|
||||
extern data2 cfig;
|
||||
|
@ -164,7 +165,7 @@ static void sendAllLists(data19 *req) {
|
|||
|
||||
#pragma clang diagnostic push
|
||||
#pragma ide diagnostic ignored "cert-err34-c"
|
||||
int getHwAddr(char *buff, char *mac) {
|
||||
int getHwAddr(char *buff, char *mac) {
|
||||
if (buff == nullptr || mac == nullptr) {
|
||||
return -1;
|
||||
}
|
||||
|
@ -234,69 +235,47 @@ sockaddr_in get_cliAddr(char *nicif, char *tempbuff, data9 *req) {
|
|||
return cliAddr;
|
||||
}
|
||||
|
||||
static void response_complete(void *user_data) {
|
||||
auto *req = (data19 *)user_data;
|
||||
if (req) {
|
||||
|
||||
static void opendhcp_http_info(http_request *request, hw_http_response *response, void *user_data) {
|
||||
hw_string status_code;
|
||||
hw_string content_type_name;
|
||||
hw_string content_type_value;
|
||||
hw_string body;
|
||||
hw_string keep_alive_name;
|
||||
hw_string keep_alive_value;
|
||||
if (req->dp) {
|
||||
free(req->dp);
|
||||
}
|
||||
|
||||
static data19 req;
|
||||
memset(&req, 0, sizeof(struct data19));
|
||||
|
||||
SETSTRING(content_type_name, "Content-Type");
|
||||
SETSTRING(content_type_value, "text/html");
|
||||
hw_set_response_header(response, &content_type_name, &content_type_value);
|
||||
|
||||
if(request->method != HW_HTTP_GET) {
|
||||
SETSTRING(status_code, HTTP_STATUS_405);
|
||||
SETSTRING(body, HTTP_STATUS_405);
|
||||
} else {
|
||||
SETSTRING(status_code, HTTP_STATUS_200);
|
||||
prepareUserHtmlRespStatus(&req);
|
||||
SETSTRING(body, req.dp);
|
||||
free(req);
|
||||
}
|
||||
hw_set_body(response, &body);
|
||||
hw_set_response_status_code(response, &status_code);
|
||||
|
||||
if (request->keep_alive) {
|
||||
SETSTRING(keep_alive_name, "Connection");
|
||||
SETSTRING(keep_alive_value, "close");
|
||||
hw_set_response_header(response, &keep_alive_name, &keep_alive_value);
|
||||
} else {
|
||||
hw_set_http_version(response, 1, 0);
|
||||
}
|
||||
|
||||
hw_http_response_send(response, user_data, nullptr);
|
||||
|
||||
free(req.dp);
|
||||
}
|
||||
|
||||
static void opendhcp_http_get_userinfo(http_request *request, hw_http_response *response, void *user_data) {
|
||||
hw_string status_code;
|
||||
hw_string content_type_name;
|
||||
hw_string content_type_value;
|
||||
hw_string body;
|
||||
hw_string keep_alive_name;
|
||||
hw_string keep_alive_value;
|
||||
static void opendhcp_http_info(http_request *request, hw_http_response *response, void *UNUSED(user_data)) {
|
||||
hw_string status_code;
|
||||
hw_string content_type_name;
|
||||
hw_string content_type_value;
|
||||
hw_string body;
|
||||
hw_string keep_alive_name;
|
||||
hw_string keep_alive_value;
|
||||
|
||||
static data19 req;
|
||||
memset(&req, 0, sizeof(struct data19));
|
||||
auto *req = (data19 *)malloc(sizeof(struct data19));
|
||||
|
||||
if(req == nullptr) {
|
||||
hw_http_response_send_error(response, HTTP_STATUS_500, "memory error");
|
||||
return;
|
||||
}
|
||||
|
||||
if (request->method != HW_HTTP_GET) {
|
||||
hw_http_response_send_error(response, HTTP_STATUS_405, HTTP_STATUS_405);
|
||||
return;
|
||||
}
|
||||
|
||||
memset(req, 0, sizeof(struct data19));
|
||||
|
||||
SETSTRING(content_type_name, "Content-Type");
|
||||
SETSTRING(content_type_value, "text/html");
|
||||
hw_set_response_header(response, &content_type_name, &content_type_value);
|
||||
|
||||
if(request->method != HW_HTTP_GET) {
|
||||
SETSTRING(status_code, HTTP_STATUS_405);
|
||||
SETSTRING(body, HTTP_STATUS_405);
|
||||
} else {
|
||||
SETSTRING(status_code, HTTP_STATUS_200);
|
||||
sendUserList(&req, request->body->value);
|
||||
SETSTRING(body, req.dp);
|
||||
}
|
||||
SETSTRING(status_code, HTTP_STATUS_200);
|
||||
prepareUserHtmlRespStatus(req);
|
||||
SETSTRING(body, req->dp);
|
||||
hw_set_body(response, &body);
|
||||
hw_set_response_status_code(response, &status_code);
|
||||
|
||||
|
@ -308,34 +287,38 @@ static void opendhcp_http_get_userinfo(http_request *request, hw_http_response *
|
|||
hw_set_http_version(response, 1, 0);
|
||||
}
|
||||
|
||||
hw_http_response_send(response, user_data, nullptr);
|
||||
|
||||
free(req.dp);
|
||||
hw_http_response_send(response, req, response_complete);
|
||||
}
|
||||
|
||||
static void opendhcp_http_get_alluser(http_request *request, hw_http_response *response, void *user_data) {
|
||||
hw_string status_code;
|
||||
hw_string content_type_name;
|
||||
hw_string content_type_value;
|
||||
hw_string body;
|
||||
hw_string keep_alive_name;
|
||||
hw_string keep_alive_value;
|
||||
static void opendhcp_http_get_userinfo(http_request *request, hw_http_response *response, void *UNUSED(user_data)) {
|
||||
hw_string status_code;
|
||||
hw_string content_type_name;
|
||||
hw_string content_type_value;
|
||||
hw_string body;
|
||||
hw_string keep_alive_name;
|
||||
hw_string keep_alive_value;
|
||||
|
||||
static data19 req;
|
||||
memset(&req, 0, sizeof(struct data19));
|
||||
auto *req = (data19 *)malloc(sizeof(struct data19));
|
||||
|
||||
if(req == nullptr) {
|
||||
hw_http_response_send_error(response, HTTP_STATUS_500, "memory error");
|
||||
return;
|
||||
}
|
||||
|
||||
if (request->method != HW_HTTP_GET) {
|
||||
hw_http_response_send_error(response, HTTP_STATUS_405, HTTP_STATUS_405);
|
||||
return;
|
||||
}
|
||||
|
||||
memset(req, 0, sizeof(struct data19));
|
||||
|
||||
SETSTRING(content_type_name, "Content-Type");
|
||||
SETSTRING(content_type_value, "text/html");
|
||||
hw_set_response_header(response, &content_type_name, &content_type_value);
|
||||
|
||||
if(request->method != HW_HTTP_GET) {
|
||||
SETSTRING(status_code, HTTP_STATUS_405);
|
||||
SETSTRING(body, HTTP_STATUS_405);
|
||||
} else {
|
||||
SETSTRING(status_code, HTTP_STATUS_200);
|
||||
sendAllLists(&req);
|
||||
SETSTRING(body, req.dp);
|
||||
}
|
||||
SETSTRING(status_code, HTTP_STATUS_200);
|
||||
sendUserList(req, request->body->value);
|
||||
SETSTRING(body, req->dp);
|
||||
hw_set_body(response, &body);
|
||||
hw_set_response_status_code(response, &status_code);
|
||||
|
||||
|
@ -347,9 +330,50 @@ static void opendhcp_http_get_alluser(http_request *request, hw_http_response *r
|
|||
hw_set_http_version(response, 1, 0);
|
||||
}
|
||||
|
||||
hw_http_response_send(response, user_data, nullptr);
|
||||
hw_http_response_send(response, req, response_complete);
|
||||
}
|
||||
|
||||
free(req.dp);
|
||||
static void opendhcp_http_get_alluser(http_request *request, hw_http_response *response, void *UNUSED(user_data)) {
|
||||
hw_string status_code;
|
||||
hw_string content_type_name;
|
||||
hw_string content_type_value;
|
||||
hw_string body;
|
||||
hw_string keep_alive_name;
|
||||
hw_string keep_alive_value;
|
||||
|
||||
auto *req = (data19 *)malloc(sizeof(struct data19));
|
||||
|
||||
if(req == nullptr) {
|
||||
hw_http_response_send_error(response, HTTP_STATUS_500, "memory error");
|
||||
return;
|
||||
}
|
||||
|
||||
if (request->method != HW_HTTP_GET) {
|
||||
hw_http_response_send_error(response, HTTP_STATUS_405, HTTP_STATUS_405);
|
||||
return;
|
||||
}
|
||||
|
||||
memset(req, 0, sizeof(struct data19));
|
||||
|
||||
SETSTRING(content_type_name, "Content-Type");
|
||||
SETSTRING(content_type_value, "text/html");
|
||||
hw_set_response_header(response, &content_type_name, &content_type_value);
|
||||
|
||||
SETSTRING(status_code, HTTP_STATUS_200);
|
||||
sendAllLists(req);
|
||||
SETSTRING(body, req->dp);
|
||||
hw_set_body(response, &body);
|
||||
hw_set_response_status_code(response, &status_code);
|
||||
|
||||
if (request->keep_alive) {
|
||||
SETSTRING(keep_alive_name, "Connection");
|
||||
SETSTRING(keep_alive_value, "close");
|
||||
hw_set_response_header(response, &keep_alive_name, &keep_alive_value);
|
||||
} else {
|
||||
hw_set_http_version(response, 1, 0);
|
||||
}
|
||||
|
||||
hw_http_response_send(response, req, response_complete);
|
||||
}
|
||||
|
||||
void opendhcp_init_http_server() {
|
||||
|
@ -357,4 +381,3 @@ void opendhcp_init_http_server() {
|
|||
hw_http_add_route("getuser", opendhcp_http_get_userinfo, nullptr);
|
||||
hw_http_add_route("allusers", opendhcp_http_get_alluser, nullptr);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue