This commit is contained in:
vraatikka
2013-07-09 09:54:07 +03:00
21 changed files with 832 additions and 89 deletions

View File

@ -53,7 +53,7 @@
*/
#define ISspace(x) isspace((int)(x))
#define HTTP_SERVER_STRING "Gateway(c) v.1.0.0\r\n"
#define HTTP_SERVER_STRING "Gateway(c) v.1.0.0"
static char *version_str = "V1.0.0";
static int httpd_read_event(DCB* dcb);
@ -65,7 +65,7 @@ static int httpd_accept(DCB *dcb);
static int httpd_close(DCB *dcb);
static int httpd_listen(DCB *dcb, char *config);
static int httpd_get_line(int sock, char *buf, int size);
static void httpd_send_headers(int client, const char *filename);
static void httpd_send_headers(DCB *dcb, int final);
/**
* The "module object" for the httpd protocol module.
@ -82,9 +82,6 @@ static GWPROTOCOL MyObject = {
httpd_listen /**< Create a listener */
};
static void
httpd_command(DCB *, char *cmd);
/**
* Implementation of the mandatory version entry point
*
@ -130,19 +127,18 @@ static int
httpd_read_event(DCB* dcb)
{
int n = -1;
GWBUF *head = NULL;
SESSION *session = dcb->session;
ROUTER_OBJECT *router = session->service->router;
ROUTER *router_instance = session->service->router_instance;
void *rsession = session->router_session;
//SESSION *session = dcb->session;
//ROUTER_OBJECT *router = session->service->router;
//ROUTER *router_instance = session->service->router_instance;
//void *rsession = session->router_session;
int numchars = 1;
char buf[1024];
char *query_string = NULL;
char *path_info = NULL;
//char *path_info = NULL;
char method[255];
char url[255];
char path[512];
//char path[512];
int cgi = 0;
size_t i, j;
GWBUF *buffer=NULL;
@ -162,21 +158,20 @@ GWBUF *buffer=NULL;
return 0;
}
if ((buffer = gwbuf_alloc(1024)) == NULL) {
//httpd_error(dcb->fd);
return 0;
}
if (strcasecmp(method, "POST") == 0)
cgi = 1;
i = 0;
while (ISspace(buf[j]) && (j < sizeof(buf)))
while (ISspace(buf[j]) && (j < sizeof(buf))) {
j++;
}
while (!ISspace(buf[j]) && (i < sizeof(url) - 1) && (j < sizeof(buf))) {
url[i] = buf[j];
i++; j++;
}
url[i] = '\0';
if (strcasecmp(method, "GET") == 0) {
@ -193,12 +188,23 @@ GWBUF *buffer=NULL;
while ((numchars > 0) && strcmp("\n", buf)) /* read & discard headers */
numchars = httpd_get_line(dcb->fd, buf, sizeof(buf));
httpd_send_headers(dcb->fd, NULL);
strcpy(GWBUF_DATA(buffer), "Welcome to HTTPD Gateway (c)\n");
/* send all the basic headers and close with \r\n */
httpd_send_headers(dcb, 1);
dcb->func.write(dcb, buffer);
if ((buffer = gwbuf_alloc(1024)) == NULL) {
//httpd_error(dcb->fd);
return 0;
}
if (strcmp(url, "/show") == 0) {
dprintAllDCBs(dcb);
} else {
dcb_printf(dcb, "Welcome to HTTPD Gateway (c) %s\n", version_str);
}
/* force the client connecton close */
dcb_close(dcb);
dcb->state = DCB_STATE_POLLING;
@ -367,63 +373,77 @@ short pnum;
}
/**
* HTTPD command implementation
*
* Called for each command in the HTTP stream.
*
* Currently we do no command execution
*
* @param dcb The client DCB
* @param cmd The command stream
* HTTPD get line from client
*/
static void
httpd_command(DCB *dcb, char *cmd)
{
}
static int httpd_get_line(int sock, char *buf, int size) {
int i = 0;
char c = '\0';
int n;
int i = 0;
char c = '\0';
int n;
while ((i < size - 1) && (c != '\n'))
{
n = recv(sock, &c, 1, 0);
/* DEBUG printf("%02X\n", c); */
if (n > 0)
{
if (c == '\r')
{
n = recv(sock, &c, 1, MSG_PEEK);
/* DEBUG printf("%02X\n", c); */
if ((n > 0) && (c == '\n'))
recv(sock, &c, 1, 0);
else
c = '\n';
}
buf[i] = c;
i++;
}
else
c = '\n';
}
buf[i] = '\0';
while ((i < size - 1) && (c != '\n')) {
n = recv(sock, &c, 1, 0);
/* DEBUG printf("%02X\n", c); */
if (n > 0) {
if (c == '\r') {
n = recv(sock, &c, 1, MSG_PEEK);
/* DEBUG printf("%02X\n", c); */
if ((n > 0) && (c == '\n'))
recv(sock, &c, 1, 0);
else
c = '\n';
}
buf[i] = c;
i++;
} else
c = '\n';
}
return(i);
buf[i] = '\0';
return i;
}
static void httpd_send_headers(int client, const char *filename)
/**
* HTTPD send basic headers with 200 OK
*/
static void httpd_send_headers(DCB *dcb, int final)
{
char buf[1024];
(void)filename; /* could use filename to determine file type */
char date[64] = "";
const char *fmt = "%a, %d %b %Y %H:%M:%S GMT";
time_t httpd_current_time = time(NULL);
strcpy(buf, "HTTP/1.0 200 OK\r\n");
send(client, buf, strlen(buf), 0);
strcpy(buf, HTTP_SERVER_STRING);
send(client, buf, strlen(buf), 0);
sprintf(buf, "Content-Type: text/html\r\n");
send(client, buf, strlen(buf), 0);
strcpy(buf, "\r\n");
send(client, buf, strlen(buf), 0);
strftime(date, sizeof(date), fmt, localtime(&httpd_current_time));
dcb_printf(dcb, "HTTP/1.1 200 OK\r\nDate: %s\r\nServer: %s\r\nConnection: close\r\nContent-Type: text/plain\r\n", date, HTTP_SERVER_STRING);
/* close the headers */
if (final) {
dcb_printf(dcb, "\r\n");
}
}
/**
* HTTPD get line from client
*/
static int httpd_dcb_readline(DCB *dcb) {
int n = -1;
char c = '\0';
char *ptr = NULL;
GWBUF *buffer = NULL;
buffer = NULL;
n = dcb_read(dcb, &buffer);
ptr = GWBUF_DATA(buffer);
while (ptr) {
c = *ptr;
ptr++;
}
buffer = gwbuf_consume(buffer, gwbuf_length(buffer));
return n;
}
///