Reformat httpd.c
This commit is contained in:
@ -43,7 +43,8 @@
|
|||||||
#include <log_manager.h>
|
#include <log_manager.h>
|
||||||
#include <resultset.h>
|
#include <resultset.h>
|
||||||
|
|
||||||
MODULE_INFO info = {
|
MODULE_INFO info =
|
||||||
|
{
|
||||||
MODULE_API_PROTOCOL,
|
MODULE_API_PROTOCOL,
|
||||||
MODULE_IN_DEVELOPMENT,
|
MODULE_IN_DEVELOPMENT,
|
||||||
GWPROTOCOL_VERSION,
|
GWPROTOCOL_VERSION,
|
||||||
@ -68,7 +69,8 @@ static void httpd_send_headers(DCB *dcb, int final);
|
|||||||
/**
|
/**
|
||||||
* The "module object" for the httpd protocol module.
|
* The "module object" for the httpd protocol module.
|
||||||
*/
|
*/
|
||||||
static GWPROTOCOL MyObject = {
|
static GWPROTOCOL MyObject =
|
||||||
|
{
|
||||||
httpd_read_event, /**< Read - EPOLLIN handler */
|
httpd_read_event, /**< Read - EPOLLIN handler */
|
||||||
httpd_write, /**< Write - data from gateway */
|
httpd_write, /**< Write - data from gateway */
|
||||||
httpd_write_event, /**< WriteReady - EPOLLOUT handler */
|
httpd_write_event, /**< WriteReady - EPOLLOUT handler */
|
||||||
@ -80,15 +82,14 @@ static GWPROTOCOL MyObject = {
|
|||||||
httpd_listen, /**< Create a listener */
|
httpd_listen, /**< Create a listener */
|
||||||
NULL, /**< Authentication */
|
NULL, /**< Authentication */
|
||||||
NULL /**< Session */
|
NULL /**< Session */
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation of the mandatory version entry point
|
* Implementation of the mandatory version entry point
|
||||||
*
|
*
|
||||||
* @return version string of the module
|
* @return version string of the module
|
||||||
*/
|
*/
|
||||||
char *
|
char* version()
|
||||||
version()
|
|
||||||
{
|
{
|
||||||
return version_str;
|
return version_str;
|
||||||
}
|
}
|
||||||
@ -97,8 +98,7 @@ version()
|
|||||||
* The module initialisation routine, called when the module
|
* The module initialisation routine, called when the module
|
||||||
* is first loaded.
|
* is first loaded.
|
||||||
*/
|
*/
|
||||||
void
|
void ModuleInit()
|
||||||
ModuleInit()
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,8 +110,7 @@ ModuleInit()
|
|||||||
*
|
*
|
||||||
* @return The module object
|
* @return The module object
|
||||||
*/
|
*/
|
||||||
GWPROTOCOL *
|
GWPROTOCOL* GetModuleObject()
|
||||||
GetModuleObject()
|
|
||||||
{
|
{
|
||||||
return &MyObject;
|
return &MyObject;
|
||||||
}
|
}
|
||||||
@ -122,23 +121,22 @@ GetModuleObject()
|
|||||||
* @param dcb The descriptor control block
|
* @param dcb The descriptor control block
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
static int
|
static int httpd_read_event(DCB* dcb)
|
||||||
httpd_read_event(DCB* dcb)
|
|
||||||
{
|
{
|
||||||
SESSION *session = dcb->session;
|
SESSION *session = dcb->session;
|
||||||
ROUTER_OBJECT *router = session->service->router;
|
ROUTER_OBJECT *router = session->service->router;
|
||||||
ROUTER *router_instance = session->service->router_instance;
|
ROUTER *router_instance = session->service->router_instance;
|
||||||
void *rsession = session->router_session;
|
void *rsession = session->router_session;
|
||||||
|
|
||||||
int numchars = 1;
|
int numchars = 1;
|
||||||
char buf[HTTPD_REQUESTLINE_MAXLEN-1] = "";
|
char buf[HTTPD_REQUESTLINE_MAXLEN-1] = "";
|
||||||
char *query_string = NULL;
|
char *query_string = NULL;
|
||||||
char method[HTTPD_METHOD_MAXLEN-1] = "";
|
char method[HTTPD_METHOD_MAXLEN-1] = "";
|
||||||
char url[HTTPD_SMALL_BUFFER] = "";
|
char url[HTTPD_SMALL_BUFFER] = "";
|
||||||
size_t i, j;
|
size_t i, j;
|
||||||
int headers_read = 0;
|
int headers_read = 0;
|
||||||
HTTPD_session *client_data = NULL;
|
HTTPD_session *client_data = NULL;
|
||||||
GWBUF *uri;
|
GWBUF *uri;
|
||||||
|
|
||||||
client_data = dcb->data;
|
client_data = dcb->data;
|
||||||
|
|
||||||
@ -150,7 +148,8 @@ GWBUF *uri;
|
|||||||
numchars = httpd_get_line(dcb->fd, buf, sizeof(buf));
|
numchars = httpd_get_line(dcb->fd, buf, sizeof(buf));
|
||||||
|
|
||||||
i = 0; j = 0;
|
i = 0; j = 0;
|
||||||
while (!ISspace(buf[j]) && (i < sizeof(method) - 1)) {
|
while (!ISspace(buf[j]) && (i < sizeof(method) - 1))
|
||||||
|
{
|
||||||
method[i] = buf[j];
|
method[i] = buf[j];
|
||||||
i++; j++;
|
i++; j++;
|
||||||
}
|
}
|
||||||
@ -159,18 +158,21 @@ GWBUF *uri;
|
|||||||
strcpy(client_data->method, method);
|
strcpy(client_data->method, method);
|
||||||
|
|
||||||
/* check allowed http methods */
|
/* check allowed http methods */
|
||||||
if (strcasecmp(method, "GET") && strcasecmp(method, "POST")) {
|
if (strcasecmp(method, "GET") && strcasecmp(method, "POST"))
|
||||||
|
{
|
||||||
//httpd_unimplemented(dcb->fd);
|
//httpd_unimplemented(dcb->fd);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
|
|
||||||
while ( (j < sizeof(buf)) && ISspace(buf[j])) {
|
while ( (j < sizeof(buf)) && ISspace(buf[j]))
|
||||||
|
{
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
|
|
||||||
while ((j < sizeof(buf) - 1) && !ISspace(buf[j]) && (i < sizeof(url) - 1)) {
|
while ((j < sizeof(buf) - 1) && !ISspace(buf[j]) && (i < sizeof(url) - 1))
|
||||||
|
{
|
||||||
url[i] = buf[j];
|
url[i] = buf[j];
|
||||||
i++; j++;
|
i++; j++;
|
||||||
}
|
}
|
||||||
@ -181,12 +183,15 @@ GWBUF *uri;
|
|||||||
* Get the query string if availble
|
* Get the query string if availble
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if (strcasecmp(method, "GET") == 0) {
|
if (strcasecmp(method, "GET") == 0)
|
||||||
|
{
|
||||||
query_string = url;
|
query_string = url;
|
||||||
while ((*query_string != '?') && (*query_string != '\0'))
|
while ((*query_string != '?') && (*query_string != '\0'))
|
||||||
|
{
|
||||||
query_string++;
|
query_string++;
|
||||||
if (*query_string == '?') {
|
}
|
||||||
|
if (*query_string == '?')
|
||||||
|
{
|
||||||
*query_string = '\0';
|
*query_string = '\0';
|
||||||
query_string++;
|
query_string++;
|
||||||
}
|
}
|
||||||
@ -196,26 +201,31 @@ GWBUF *uri;
|
|||||||
* Get the request headers
|
* Get the request headers
|
||||||
*/
|
*/
|
||||||
|
|
||||||
while ((numchars > 0) && strcmp("\n", buf)) {
|
while ((numchars > 0) && strcmp("\n", buf))
|
||||||
|
{
|
||||||
char *value = NULL;
|
char *value = NULL;
|
||||||
char *end = NULL;
|
char *end = NULL;
|
||||||
numchars = httpd_get_line(dcb->fd, buf, sizeof(buf));
|
numchars = httpd_get_line(dcb->fd, buf, sizeof(buf));
|
||||||
if ( (value = strchr(buf, ':'))) {
|
if ((value = strchr(buf, ':')))
|
||||||
|
{
|
||||||
*value = '\0';
|
*value = '\0';
|
||||||
value++;
|
value++;
|
||||||
end = &value[strlen(value) -1];
|
end = &value[strlen(value) -1];
|
||||||
*end = '\0';
|
*end = '\0';
|
||||||
|
|
||||||
if (strncasecmp(buf, "Hostname", 6) == 0) {
|
if (strncasecmp(buf, "Hostname", 6) == 0)
|
||||||
|
{
|
||||||
strcpy(client_data->hostname, value);
|
strcpy(client_data->hostname, value);
|
||||||
}
|
}
|
||||||
if (strncasecmp(buf, "useragent", 9) == 0) {
|
if (strncasecmp(buf, "useragent", 9) == 0)
|
||||||
|
{
|
||||||
strcpy(client_data->useragent, value);
|
strcpy(client_data->useragent, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (numchars) {
|
if (numchars)
|
||||||
|
{
|
||||||
headers_read = 1;
|
headers_read = 1;
|
||||||
memcpy(&client_data->headers_received, &headers_read, sizeof(int));
|
memcpy(&client_data->headers_received, &headers_read, sizeof(int));
|
||||||
}
|
}
|
||||||
@ -232,15 +242,22 @@ GWBUF *uri;
|
|||||||
* ToDO: launch proper content handling based on the requested URI, later REST interface
|
* ToDO: launch proper content handling based on the requested URI, later REST interface
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
if (strcmp(url, "/show") == 0) {
|
if (strcmp(url, "/show") == 0)
|
||||||
if (query_string && strlen(query_string)) {
|
{
|
||||||
|
if (query_string && strlen(query_string))
|
||||||
|
{
|
||||||
if (strcmp(query_string, "dcb") == 0)
|
if (strcmp(query_string, "dcb") == 0)
|
||||||
|
{
|
||||||
dprintAllDCBs(dcb);
|
dprintAllDCBs(dcb);
|
||||||
|
}
|
||||||
if (strcmp(query_string, "session") == 0)
|
if (strcmp(query_string, "session") == 0)
|
||||||
|
{
|
||||||
dprintAllSessions(dcb);
|
dprintAllSessions(dcb);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (strcmp(url, "/services") == 0) {
|
}
|
||||||
|
if (strcmp(url, "/services") == 0)
|
||||||
|
{
|
||||||
RESULTSET *set, *seviceGetList();
|
RESULTSET *set, *seviceGetList();
|
||||||
if ((set = serviceGetList()) != NULL)
|
if ((set = serviceGetList()) != NULL)
|
||||||
{
|
{
|
||||||
@ -268,8 +285,7 @@ GWBUF *uri;
|
|||||||
* @param dcb The descriptor control block
|
* @param dcb The descriptor control block
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
static int
|
static int httpd_write_event(DCB *dcb)
|
||||||
httpd_write_event(DCB *dcb)
|
|
||||||
{
|
{
|
||||||
return dcb_drain_writeq(dcb);
|
return dcb_drain_writeq(dcb);
|
||||||
}
|
}
|
||||||
@ -283,8 +299,7 @@ httpd_write_event(DCB *dcb)
|
|||||||
* @param dcb Descriptor Control Block for the socket
|
* @param dcb Descriptor Control Block for the socket
|
||||||
* @param queue Linked list of buffes to write
|
* @param queue Linked list of buffes to write
|
||||||
*/
|
*/
|
||||||
static int
|
static int httpd_write(DCB *dcb, GWBUF *queue)
|
||||||
httpd_write(DCB *dcb, GWBUF *queue)
|
|
||||||
{
|
{
|
||||||
int rc;
|
int rc;
|
||||||
rc = dcb_write(dcb, queue);
|
rc = dcb_write(dcb, queue);
|
||||||
@ -296,8 +311,7 @@ httpd_write(DCB *dcb, GWBUF *queue)
|
|||||||
*
|
*
|
||||||
* @param dcb The descriptor control block
|
* @param dcb The descriptor control block
|
||||||
*/
|
*/
|
||||||
static int
|
static int httpd_error(DCB *dcb)
|
||||||
httpd_error(DCB *dcb)
|
|
||||||
{
|
{
|
||||||
dcb_close(dcb);
|
dcb_close(dcb);
|
||||||
return 0;
|
return 0;
|
||||||
@ -308,8 +322,7 @@ httpd_error(DCB *dcb)
|
|||||||
*
|
*
|
||||||
* @param dcb The descriptor control block
|
* @param dcb The descriptor control block
|
||||||
*/
|
*/
|
||||||
static int
|
static int httpd_hangup(DCB *dcb)
|
||||||
httpd_hangup(DCB *dcb)
|
|
||||||
{
|
{
|
||||||
dcb_close(dcb);
|
dcb_close(dcb);
|
||||||
return 0;
|
return 0;
|
||||||
@ -321,10 +334,9 @@ httpd_hangup(DCB *dcb)
|
|||||||
*
|
*
|
||||||
* @param dcb The descriptor control block
|
* @param dcb The descriptor control block
|
||||||
*/
|
*/
|
||||||
static int
|
static int httpd_accept(DCB *dcb)
|
||||||
httpd_accept(DCB *dcb)
|
|
||||||
{
|
{
|
||||||
int n_connect = 0;
|
int n_connect = 0;
|
||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
@ -335,12 +347,15 @@ int n_connect = 0;
|
|||||||
HTTPD_session *client_data = NULL;
|
HTTPD_session *client_data = NULL;
|
||||||
|
|
||||||
if ((so = accept(dcb->fd, (struct sockaddr *)&addr, &addrlen)) == -1)
|
if ((so = accept(dcb->fd, (struct sockaddr *)&addr, &addrlen)) == -1)
|
||||||
|
{
|
||||||
return n_connect;
|
return n_connect;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
atomic_add(&dcb->stats.n_accepts, 1);
|
atomic_add(&dcb->stats.n_accepts, 1);
|
||||||
|
|
||||||
if((client = dcb_alloc(DCB_ROLE_REQUEST_HANDLER))){
|
if ((client = dcb_alloc(DCB_ROLE_REQUEST_HANDLER)))
|
||||||
|
{
|
||||||
client->fd = so;
|
client->fd = so;
|
||||||
client->remote = strdup(inet_ntoa(addr.sin_addr));
|
client->remote = strdup(inet_ntoa(addr.sin_addr));
|
||||||
memcpy(&client->func, &MyObject, sizeof(GWPROTOCOL));
|
memcpy(&client->func, &MyObject, sizeof(GWPROTOCOL));
|
||||||
@ -349,8 +364,7 @@ int n_connect = 0;
|
|||||||
client_data = (HTTPD_session *)calloc(1, sizeof(HTTPD_session));
|
client_data = (HTTPD_session *)calloc(1, sizeof(HTTPD_session));
|
||||||
client->data = client_data;
|
client->data = client_data;
|
||||||
|
|
||||||
client->session =
|
client->session = session_alloc(dcb->session->service, client);
|
||||||
session_alloc(dcb->session->service, client);
|
|
||||||
|
|
||||||
if (NULL == client->session || poll_add_dcb(client) == -1)
|
if (NULL == client->session || poll_add_dcb(client) == -1)
|
||||||
{
|
{
|
||||||
@ -377,8 +391,7 @@ int n_connect = 0;
|
|||||||
* @param dcb The descriptor control block
|
* @param dcb The descriptor control block
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static int
|
static int httpd_close(DCB *dcb)
|
||||||
httpd_close(DCB *dcb)
|
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -389,17 +402,18 @@ httpd_close(DCB *dcb)
|
|||||||
* @param listener The Listener DCB
|
* @param listener The Listener DCB
|
||||||
* @param config Configuration (ip:port)
|
* @param config Configuration (ip:port)
|
||||||
*/
|
*/
|
||||||
static int
|
static int httpd_listen(DCB *listener, char *config)
|
||||||
httpd_listen(DCB *listener, char *config)
|
|
||||||
{
|
{
|
||||||
struct sockaddr_in addr;
|
struct sockaddr_in addr;
|
||||||
int one = 1;
|
int one = 1;
|
||||||
int rc;
|
int rc;
|
||||||
int syseno = 0;
|
int syseno = 0;
|
||||||
|
|
||||||
memcpy(&listener->func, &MyObject, sizeof(GWPROTOCOL));
|
memcpy(&listener->func, &MyObject, sizeof(GWPROTOCOL));
|
||||||
if (!parse_bindconfig(config, 6442, &addr))
|
if (!parse_bindconfig(config, 6442, &addr))
|
||||||
|
{
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
if ((listener->fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
if ((listener->fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
||||||
{
|
{
|
||||||
@ -413,7 +427,8 @@ int syseno = 0;
|
|||||||
(char *)&one,
|
(char *)&one,
|
||||||
sizeof(one));
|
sizeof(one));
|
||||||
|
|
||||||
if(syseno != 0){
|
if (syseno != 0)
|
||||||
|
{
|
||||||
char errbuf[STRERROR_BUFLEN];
|
char errbuf[STRERROR_BUFLEN];
|
||||||
MXS_ERROR("Failed to set socket options. Error %d: %s",
|
MXS_ERROR("Failed to set socket options. Error %d: %s",
|
||||||
errno, strerror_r(errno, errbuf, sizeof(errbuf)));
|
errno, strerror_r(errno, errbuf, sizeof(errbuf)));
|
||||||
@ -430,9 +445,12 @@ int syseno = 0;
|
|||||||
|
|
||||||
rc = listen(listener->fd, SOMAXCONN);
|
rc = listen(listener->fd, SOMAXCONN);
|
||||||
|
|
||||||
if (rc == 0) {
|
if (rc == 0)
|
||||||
|
{
|
||||||
MXS_NOTICE("Listening httpd connections at %s", config);
|
MXS_NOTICE("Listening httpd connections at %s", config);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
int eno = errno;
|
int eno = errno;
|
||||||
errno = 0;
|
errno = 0;
|
||||||
char errbuf[STRERROR_BUFLEN];
|
char errbuf[STRERROR_BUFLEN];
|
||||||
@ -443,7 +461,6 @@ int syseno = 0;
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (poll_add_dcb(listener) == -1)
|
if (poll_add_dcb(listener) == -1)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
@ -454,29 +471,39 @@ int syseno = 0;
|
|||||||
/**
|
/**
|
||||||
* HTTPD get line from client
|
* HTTPD get line from client
|
||||||
*/
|
*/
|
||||||
static int httpd_get_line(int sock, char *buf, int size) {
|
static int httpd_get_line(int sock, char *buf, int size)
|
||||||
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
char c = '\0';
|
char c = '\0';
|
||||||
int n;
|
int n;
|
||||||
|
|
||||||
while ((i < size - 1) && (c != '\n')) {
|
while ((i < size - 1) && (c != '\n'))
|
||||||
|
{
|
||||||
n = recv(sock, &c, 1, 0);
|
n = recv(sock, &c, 1, 0);
|
||||||
/* DEBUG printf("%02X\n", c); */
|
/* DEBUG printf("%02X\n", c); */
|
||||||
if (n > 0) {
|
if (n > 0)
|
||||||
if (c == '\r') {
|
{
|
||||||
|
if (c == '\r')
|
||||||
|
{
|
||||||
n = recv(sock, &c, 1, MSG_PEEK);
|
n = recv(sock, &c, 1, MSG_PEEK);
|
||||||
/* DEBUG printf("%02X\n", c); */
|
/* DEBUG printf("%02X\n", c); */
|
||||||
if ((n > 0) && (c == '\n')) {
|
if ((n > 0) && (c == '\n'))
|
||||||
if(recv(sock, &c, 1, 0) < 0){
|
{
|
||||||
|
if (recv(sock, &c, 1, 0) < 0)
|
||||||
|
{
|
||||||
c = '\n';
|
c = '\n';
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
c = '\n';
|
c = '\n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
buf[i] = c;
|
buf[i] = c;
|
||||||
i++;
|
i++;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
c = '\n';
|
c = '\n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -499,10 +526,14 @@ static void httpd_send_headers(DCB *dcb, int final)
|
|||||||
localtime_r(&httpd_current_time, &tm);
|
localtime_r(&httpd_current_time, &tm);
|
||||||
strftime(date, sizeof(date), fmt, &tm);
|
strftime(date, sizeof(date), fmt, &tm);
|
||||||
|
|
||||||
dcb_printf(dcb, "HTTP/1.1 200 OK\r\nDate: %s\r\nServer: %s\r\nConnection: close\r\nContent-Type: application/json\r\n", date, HTTP_SERVER_STRING);
|
dcb_printf(dcb,
|
||||||
|
"HTTP/1.1 200 OK\r\nDate: %s\r\nServer: %s\r\nConnection: "
|
||||||
|
"close\r\nContent-Type: application/json\r\n",
|
||||||
|
date, HTTP_SERVER_STRING);
|
||||||
|
|
||||||
/* close the headers */
|
/* close the headers */
|
||||||
if (final) {
|
if (final)
|
||||||
|
{
|
||||||
dcb_printf(dcb, "\r\n");
|
dcb_printf(dcb, "\r\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user