Reformat httpd.c

This commit is contained in:
Johan Wikman
2016-01-12 14:14:40 +02:00
parent 040c67127a
commit 018b87d304

View File

@ -25,14 +25,14 @@
* databases. * databases.
* *
* In the first instance it is intended to allow a debug connection to access * In the first instance it is intended to allow a debug connection to access
* internal data structures, however it may also be used to manage the * internal data structures, however it may also be used to manage the
* configuration of the gateway via REST interface. * configuration of the gateway via REST interface.
* *
* @verbatim * @verbatim
* Revision History * Revision History
* Date Who Description * Date Who Description
* 08/07/2013 Massimiliano Pinto Initial version * 08/07/2013 Massimiliano Pinto Initial version
* 09/07/2013 Massimiliano Pinto Added /show?dcb|session for all dcbs|sessions * 09/07/2013 Massimiliano Pinto Added /show?dcb|session for all dcbs|sessions
* *
* @endverbatim * @endverbatim
*/ */
@ -43,11 +43,12 @@
#include <log_manager.h> #include <log_manager.h>
#include <resultset.h> #include <resultset.h>
MODULE_INFO info = { MODULE_INFO info =
MODULE_API_PROTOCOL, {
MODULE_IN_DEVELOPMENT, MODULE_API_PROTOCOL,
GWPROTOCOL_VERSION, MODULE_IN_DEVELOPMENT,
"An experimental HTTPD implementation for use in admnistration" GWPROTOCOL_VERSION,
"An experimental HTTPD implementation for use in admnistration"
}; };
#define ISspace(x) isspace((int)(x)) #define ISspace(x) isspace((int)(x))
@ -68,37 +69,36 @@ 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_write, /**< Write - data from gateway */ httpd_read_event, /**< Read - EPOLLIN handler */
httpd_write_event, /**< WriteReady - EPOLLOUT handler */ httpd_write, /**< Write - data from gateway */
httpd_error, /**< Error - EPOLLERR handler */ httpd_write_event, /**< WriteReady - EPOLLOUT handler */
httpd_hangup, /**< HangUp - EPOLLHUP handler */ httpd_error, /**< Error - EPOLLERR handler */
httpd_accept, /**< Accept */ httpd_hangup, /**< HangUp - EPOLLHUP handler */
NULL, /**< Connect */ httpd_accept, /**< Accept */
httpd_close, /**< Close */ NULL, /**< Connect */
httpd_listen, /**< Create a listener */ httpd_close, /**< Close */
NULL, /**< Authentication */ httpd_listen, /**< Create a listener */
NULL /**< Session */ NULL, /**< Authentication */
}; 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;
} }
/** /**
* 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,168 +110,184 @@ ModuleInit()
* *
* @return The module object * @return The module object
*/ */
GWPROTOCOL * GWPROTOCOL* GetModuleObject()
GetModuleObject()
{ {
return &MyObject; return &MyObject;
} }
/** /**
* Read event for EPOLLIN on the httpd protocol module. * Read event for EPOLLIN on the httpd protocol module.
* *
* @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;
/** /**
* get the request line * get the request line
* METHOD URL HTTP_VER\r\n * METHOD URL HTTP_VER\r\n
*/ */
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]; {
i++; j++; method[i] = buf[j];
} i++; j++;
method[i] = '\0'; }
method[i] = '\0';
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); {
return 0; //httpd_unimplemented(dcb->fd);
} 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]; {
i++; j++; url[i] = buf[j];
} i++; j++;
}
url[i] = '\0'; url[i] = '\0';
/** /**
* 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; {
while ((*query_string != '?') && (*query_string != '\0')) query_string = url;
query_string++; while ((*query_string != '?') && (*query_string != '\0'))
if (*query_string == '?') { {
query_string++;
}
if (*query_string == '?')
{
*query_string = '\0';
query_string++;
}
}
*query_string = '\0'; /**
query_string++; * Get the request headers
} */
}
/** while ((numchars > 0) && strcmp("\n", buf))
* Get the request headers {
*/ char *value = NULL;
char *end = NULL;
numchars = httpd_get_line(dcb->fd, buf, sizeof(buf));
if ((value = strchr(buf, ':')))
{
*value = '\0';
value++;
end = &value[strlen(value) -1];
*end = '\0';
while ((numchars > 0) && strcmp("\n", buf)) { if (strncasecmp(buf, "Hostname", 6) == 0)
char *value = NULL; {
char *end = NULL; strcpy(client_data->hostname, value);
numchars = httpd_get_line(dcb->fd, buf, sizeof(buf)); }
if ( (value = strchr(buf, ':'))) { if (strncasecmp(buf, "useragent", 9) == 0)
*value = '\0'; {
value++; strcpy(client_data->useragent, value);
end = &value[strlen(value) -1]; }
*end = '\0'; }
}
if (strncasecmp(buf, "Hostname", 6) == 0) { if (numchars)
strcpy(client_data->hostname, value); {
} headers_read = 1;
if (strncasecmp(buf, "useragent", 9) == 0) { memcpy(&client_data->headers_received, &headers_read, sizeof(int));
strcpy(client_data->useragent, value); }
}
}
}
if (numchars) { /**
headers_read = 1; * Now begins the server reply
memcpy(&client_data->headers_received, &headers_read, sizeof(int)); */
}
/** /* send all the basic headers and close with \r\n */
* Now begins the server reply httpd_send_headers(dcb, 1);
*/
/* send all the basic headers and close with \r\n */
httpd_send_headers(dcb, 1);
#if 0 #if 0
/** /**
* 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 (strcmp(query_string, "dcb") == 0) if (query_string && strlen(query_string))
dprintAllDCBs(dcb); {
if (strcmp(query_string, "session") == 0) if (strcmp(query_string, "dcb") == 0)
dprintAllSessions(dcb); {
} dprintAllDCBs(dcb);
} }
if (strcmp(url, "/services") == 0) { if (strcmp(query_string, "session") == 0)
RESULTSET *set, *seviceGetList(); {
if ((set = serviceGetList()) != NULL) dprintAllSessions(dcb);
{ }
resultset_stream_json(set, dcb); }
resultset_free(set); }
} if (strcmp(url, "/services") == 0)
} {
RESULTSET *set, *seviceGetList();
if ((set = serviceGetList()) != NULL)
{
resultset_stream_json(set, dcb);
resultset_free(set);
}
}
#endif #endif
if ((uri = gwbuf_alloc(strlen(url) + 1)) != NULL) if ((uri = gwbuf_alloc(strlen(url) + 1)) != NULL)
{ {
strcpy((char *)GWBUF_DATA(uri), url); strcpy((char *)GWBUF_DATA(uri), url);
gwbuf_set_type(uri, GWBUF_TYPE_HTTP); gwbuf_set_type(uri, GWBUF_TYPE_HTTP);
SESSION_ROUTE_QUERY(session, uri); SESSION_ROUTE_QUERY(session, uri);
} }
/* force the client connecton close */ /* force the client connecton close */
dcb_close(dcb); dcb_close(dcb);
return 0; return 0;
} }
/** /**
* EPOLLOUT handler for the HTTPD protocol module. * EPOLLOUT handler for the HTTPD protocol module.
* *
* @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);
} }
/** /**
@ -280,210 +296,221 @@ httpd_write_event(DCB *dcb)
* Writes the content of the buffer queue to the socket * Writes the content of the buffer queue to the socket
* observing the non-blocking principles of the gateway. * observing the non-blocking principles of the gateway.
* *
* @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);
return rc; return rc;
} }
/** /**
* Handler for the EPOLLERR event. * Handler for the EPOLLERR event.
* *
* @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;
} }
/** /**
* Handler for the EPOLLHUP event. * Handler for the EPOLLHUP event.
* *
* @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;
} }
/** /**
* Handler for the EPOLLIN event when the DCB refers to the listening * Handler for the EPOLLIN event when the DCB refers to the listening
* socket for the protocol. * socket for the protocol.
* *
* @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)
{ {
int so = -1; int so = -1;
struct sockaddr_in addr; struct sockaddr_in addr;
socklen_t addrlen; socklen_t addrlen;
DCB *client = NULL; DCB *client = NULL;
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; {
else return n_connect;
{ }
atomic_add(&dcb->stats.n_accepts, 1); else
{
if((client = dcb_alloc(DCB_ROLE_REQUEST_HANDLER))){ atomic_add(&dcb->stats.n_accepts, 1);
client->fd = so;
client->remote = strdup(inet_ntoa(addr.sin_addr));
memcpy(&client->func, &MyObject, sizeof(GWPROTOCOL));
/* create the session data for HTTPD */ if ((client = dcb_alloc(DCB_ROLE_REQUEST_HANDLER)))
client_data = (HTTPD_session *)calloc(1, sizeof(HTTPD_session)); {
client->data = client_data; client->fd = so;
client->remote = strdup(inet_ntoa(addr.sin_addr));
client->session = memcpy(&client->func, &MyObject, sizeof(GWPROTOCOL));
session_alloc(dcb->session->service, client);
if (NULL == client->session || poll_add_dcb(client) == -1) /* create the session data for HTTPD */
{ client_data = (HTTPD_session *)calloc(1, sizeof(HTTPD_session));
close(so); client->data = client_data;
dcb_close(client);
return n_connect; client->session = session_alloc(dcb->session->service, client);
}
n_connect++; if (NULL == client->session || poll_add_dcb(client) == -1)
} {
else close(so);
{ dcb_close(client);
close(so); return n_connect;
} }
} n_connect++;
} }
else
return n_connect; {
close(so);
}
}
}
return n_connect;
} }
/** /**
* The close handler for the descriptor. Called by the gateway to * The close handler for the descriptor. Called by the gateway to
* explicitly close a connection. * explicitly close a connection.
* *
* @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;
} }
/** /**
* HTTTP daemon listener entry point * HTTTP daemon listener entry point
* *
* @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)
{ {
return 0; return 0;
} }
/* socket options */ /* socket options */
syseno = setsockopt(listener->fd, syseno = setsockopt(listener->fd,
SOL_SOCKET, SOL_SOCKET,
SO_REUSEADDR, SO_REUSEADDR,
(char *)&one, (char *)&one,
sizeof(one)); sizeof(one));
if(syseno != 0){ if (syseno != 0)
char errbuf[STRERROR_BUFLEN]; {
MXS_ERROR("Failed to set socket options. Error %d: %s", char errbuf[STRERROR_BUFLEN];
errno, strerror_r(errno, errbuf, sizeof(errbuf))); MXS_ERROR("Failed to set socket options. Error %d: %s",
return 0; errno, strerror_r(errno, errbuf, sizeof(errbuf)));
} return 0;
/* set NONBLOCKING mode */ }
setnonblocking(listener->fd); /* set NONBLOCKING mode */
setnonblocking(listener->fd);
/* bind address and port */ /* bind address and port */
if (bind(listener->fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) if (bind(listener->fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{ {
return 0; return 0;
} }
rc = listen(listener->fd, SOMAXCONN); rc = listen(listener->fd, SOMAXCONN);
if (rc == 0) {
MXS_NOTICE("Listening httpd connections at %s", config);
} else {
int eno = errno;
errno = 0;
char errbuf[STRERROR_BUFLEN];
fprintf(stderr,
"\n* Failed to start listening http due error %d, %s\n\n",
eno,
strerror_r(eno, errbuf, sizeof(errbuf)));
return 0;
}
if (rc == 0)
if (poll_add_dcb(listener) == -1) {
{ MXS_NOTICE("Listening httpd connections at %s", config);
return 0; }
} else
return 1; {
int eno = errno;
errno = 0;
char errbuf[STRERROR_BUFLEN];
fprintf(stderr,
"\n* Failed to start listening http due error %d, %s\n\n",
eno,
strerror_r(eno, errbuf, sizeof(errbuf)));
return 0;
}
if (poll_add_dcb(listener) == -1)
{
return 0;
}
return 1;
} }
/** /**
* 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; {
char c = '\0'; int i = 0;
int n; char c = '\0';
int n;
while ((i < size - 1) && (c != '\n')) { while ((i < size - 1) && (c != '\n'))
n = recv(sock, &c, 1, 0); {
/* DEBUG printf("%02X\n", c); */ n = recv(sock, &c, 1, 0);
if (n > 0) { /* DEBUG printf("%02X\n", c); */
if (c == '\r') { if (n > 0)
n = recv(sock, &c, 1, MSG_PEEK); {
/* DEBUG printf("%02X\n", c); */ if (c == '\r')
if ((n > 0) && (c == '\n')) { {
if(recv(sock, &c, 1, 0) < 0){ n = recv(sock, &c, 1, MSG_PEEK);
c = '\n'; /* DEBUG printf("%02X\n", c); */
} if ((n > 0) && (c == '\n'))
} else { {
c = '\n'; if (recv(sock, &c, 1, 0) < 0)
} {
} c = '\n';
buf[i] = c; }
i++; }
} else { else
c = '\n'; {
} c = '\n';
} }
}
buf[i] = c;
i++;
}
else
{
c = '\n';
}
}
buf[i] = '\0'; buf[i] = '\0';
return i; return i;
} }
/** /**
@ -491,18 +518,22 @@ static int httpd_get_line(int sock, char *buf, int size) {
*/ */
static void httpd_send_headers(DCB *dcb, int final) static void httpd_send_headers(DCB *dcb, int final)
{ {
char date[64] = ""; char date[64] = "";
const char *fmt = "%a, %d %b %Y %H:%M:%S GMT"; const char *fmt = "%a, %d %b %Y %H:%M:%S GMT";
time_t httpd_current_time = time(NULL); time_t httpd_current_time = time(NULL);
struct tm tm; struct tm tm;
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");
}
} }