diff --git a/http_server.c b/http_server.c index 71a84a6..3247bd5 100644 --- a/http_server.c +++ b/http_server.c @@ -86,7 +86,7 @@ void accept_request(int client) } else { - headers(client,mime_type); + ctype(client,mime_type); // if the mime is supported, send the file serve_file(client, path); } @@ -95,25 +95,6 @@ void accept_request(int client) close(client); } -/**********************************************************************/ -/* Inform the client that a request it has made has a problem. -* Parameters: client socket */ -/**********************************************************************/ -void bad_request(int client) -{ - char buf[1024]; - - sprintf(buf, "HTTP/1.0 400 BAD REQUEST\r\n"); - send(client, buf, sizeof(buf), 0); - sprintf(buf, "Content-type: text/html\r\n"); - send(client, buf, sizeof(buf), 0); - sprintf(buf, "\r\n"); - send(client, buf, sizeof(buf), 0); - sprintf(buf, "
Your browser sent a bad request, "); - send(client, buf, sizeof(buf), 0); - sprintf(buf, "such as a POST without a Content-Length.\r\n"); - send(client, buf, sizeof(buf), 0); -} /**********************************************************************/ /* Put the entire contents of a file out on a socket. This function @@ -122,27 +103,6 @@ void bad_request(int client) * Parameters: the client socket descriptor * FILE pointer for the file to cat */ /**********************************************************************/ -void __b(int client, const unsigned char* data, int size) -{ - char buf[BUFFLEN]; - int sent = 0; - int buflen = 0; - if(size <= BUFFLEN) - send(client,data,size,0); - else - { - while(sent < size) - { - if(size - sent > BUFFLEN) - buflen = BUFFLEN; - else - buflen = size - sent; - memcpy(buf,data+sent,buflen); - send(client,buf,buflen,0); - sent += buflen; - } - } -} void catb(int client, FILE* ptr) { unsigned char buffer[BUFFLEN]; @@ -175,16 +135,11 @@ void cat(int client, FILE *resource) /**********************************************************************/ void cannot_execute(int client) { - char buf[1024]; - - sprintf(buf, "HTTP/1.0 500 Internal Server Error\r\n"); - send(client, buf, strlen(buf), 0); - sprintf(buf, "Content-type: text/html\r\n"); - send(client, buf, strlen(buf), 0); - sprintf(buf, "\r\n"); - send(client, buf, strlen(buf), 0); - sprintf(buf, "
Error prohibited CGI execution.\r\n"); - send(client, buf, strlen(buf), 0); + set_status(client,500,"Internal Server Error"); + __t(client,SERVER_STRING); + __t(client,"Content-Type: text/html"); + response(client,""); + __t(client, "
Error prohibited CGI execution."); } /**********************************************************************/ @@ -243,50 +198,21 @@ int get_line(int sock, char *buf, int size) return(i); } -/**********************************************************************/ -/* Return the informational HTTP headers about a file. */ -/* Parameters: the socket to print the headers on -* the name of the file */ -/**********************************************************************/ -void headers(int client, const char *mime_type) -{ - char buf[1024]; - //printf("Mime %s\n", mime(filename)); - strcpy(buf, "HTTP/1.0 200 OK\r\n"); - send(client, buf, strlen(buf), 0); - strcpy(buf, SERVER_STRING); - send(client, buf, strlen(buf), 0); - sprintf(buf, "Content-Type: %s\r\n",mime_type); - send(client, buf, strlen(buf), 0); - strcpy(buf, "\r\n"); - send(client, buf, strlen(buf), 0); -} /**********************************************************************/ /* Give a client a 404 not found status message. */ /**********************************************************************/ void not_found(int client) { - char buf[1024]; - - sprintf(buf, "HTTP/1.0 404 NOT FOUND\r\n"); - send(client, buf, strlen(buf), 0); - sprintf(buf, SERVER_STRING); - send(client, buf, strlen(buf), 0); - sprintf(buf, "Content-Type: text/html\r\n"); - send(client, buf, strlen(buf), 0); - sprintf(buf, "\r\n"); - send(client, buf, strlen(buf), 0); - sprintf(buf, "
The server could not fulfill\r\n"); - send(client, buf, strlen(buf), 0); - sprintf(buf, "your request because the resource specified\r\n"); - send(client, buf, strlen(buf), 0); - sprintf(buf, "is unavailable or nonexistent.\r\n"); - send(client, buf, strlen(buf), 0); - sprintf(buf, "\r\n"); - send(client, buf, strlen(buf), 0); + set_status(client,404,"NOT FOUND"); + __t(client,SERVER_STRING); + __t(client,"Content-Type: text/html"); + response(client,""); + __t(client, "
The server could not fulfill"); + __t(client, "your request because the resource specified"); + __t(client, "is unavailable or nonexistent."); + __t(client, ""); } /**********************************************************************/ @@ -360,24 +286,14 @@ int startup(unsigned *port) /**********************************************************************/ void unimplemented(int client) { - char buf[1024]; - - sprintf(buf, "HTTP/1.0 501 Method Not Implemented\r\n"); - send(client, buf, strlen(buf), 0); - sprintf(buf, SERVER_STRING); - send(client, buf, strlen(buf), 0); - sprintf(buf, "Content-Type: text/html\r\n"); - send(client, buf, strlen(buf), 0); - sprintf(buf, "\r\n"); - send(client, buf, strlen(buf), 0); - sprintf(buf, "
HTTP request method not supported.\r\n"); - send(client, buf, strlen(buf), 0); - sprintf(buf, "\r\n"); - send(client, buf, strlen(buf), 0); + set_status(client,501,"Method Not Implemented"); + __t(client,SERVER_STRING); + __t(client,"Content-Type: text/html"); + response(client,""); + __t(client, "
HTTP request method not supported.");
+ __t(client, "");
}
@@ -780,46 +696,6 @@ char* json_data_decode(int client,int len)
return query;
}
-/**
- * read the request as a string line format
- * @param sock socket
- * @return a request string
- */
-char* read_line(int sock)
-{
- char buf[BUFFLEN];
- read_buf(sock,buf,sizeof(buf));
- return strdup(buf);
-}
-/**
- * Read the socket request in to a buffer or size
- * The data is read until the buffer is full or
- * there are a carrier return character
- * @param sock socket
- * @param buf buffer
- * @param size size of buffer
- * @return number of bytes read
- */
-int read_buf(int sock, char*buf,int size)
-{
- int i = 0;
- char c = '\0';
- int n;
- while ((i < size - 1) && (c != '\n'))
- {
- n = recv(sock, &c, 1, 0);
- if (n > 0)
- {
- buf[i] = c;
- i++;
- }
- else
- c = '\n';
- }
- buf[i] = '\0';
- return i;
-}
-
/**
* Execute a plugin based on the http requeset
* First decode the http request header to find the correct plugin
diff --git a/http_server.h b/http_server.h
index f1990ac..91e3bc8 100644
--- a/http_server.h
+++ b/http_server.h
@@ -17,15 +17,13 @@
#define ISspace(x) isspace((int)(x))
-#define SERVER_STRING "Server: ant-httpd\r\n"
+#define SERVER_STRING "Server: ant-httpd"
void accept_request(int);
-void bad_request(int);
void cat(int, FILE *);
void cannot_execute(int);
void error_die(const char *);
int get_line(int, char *, int);
-void headers(int, const char *);
void not_found(int);
void serve_file(int, const char *);
int startup(unsigned *);
@@ -38,8 +36,6 @@ dictionary decode_request(int client,const char* method,const char* query);
dictionary decode_multi_part_request(int,const char*);
dictionary decode_cookie(const char*);
char* json_data_decode(int,int);
-char* read_line(int sock);
-int read_buf(int sock,char* buf,int i);
int execute_plugin(int client, const char *path,
const char *method, const char *query_string);
\ No newline at end of file
diff --git a/plugins/handle.c b/plugins/handle.c
index 0be636b..09568db 100644
--- a/plugins/handle.c
+++ b/plugins/handle.c
@@ -214,4 +214,43 @@ void unknow(int client)
int ws_enable(dictionary dic)
{
return (dic != NULL && R_INT(dic,"__web_socket__") == 1);
+}
+/**
+ * read the request as a string line format
+ * @param sock socket
+ * @return a request string
+ */
+char* read_line(int sock)
+{
+ char buf[BUFFLEN];
+ read_buf(sock,buf,sizeof(buf));
+ return strdup(buf);
+}
+/**
+ * Read the socket request in to a buffer or size
+ * The data is read until the buffer is full or
+ * there are a carrier return character
+ * @param sock socket
+ * @param buf buffer
+ * @param size size of buffer
+ * @return number of bytes read
+ */
+int read_buf(int sock, char*buf,int size)
+{
+ int i = 0;
+ char c = '\0';
+ int n;
+ while ((i < size - 1) && (c != '\n'))
+ {
+ n = recv(sock, &c, 1, 0);
+ if (n > 0)
+ {
+ buf[i] = c;
+ i++;
+ }
+ else
+ c = '\n';
+ }
+ buf[i] = '\0';
+ return i;
}
\ No newline at end of file
diff --git a/plugins/handle.h b/plugins/handle.h
index 4134b42..b2d2fd2 100644
--- a/plugins/handle.h
+++ b/plugins/handle.h
@@ -9,7 +9,6 @@
#include "dictionary.h"
#include "list.h"
#include "ini.h"
-#include "ws.h"
#define SERVER_NAME "antd"
#define IS_POST(method) (strcmp(method,"POST")== 0)
@@ -45,4 +44,6 @@ void clear_cookie(int, dictionary);
/*Default function for plugin*/
void unknow(int);
int ws_enable(dictionary);
+char* read_line(int sock);
+int read_buf(int sock,char* buf,int i);
#endif
diff --git a/plugins/plugin.h b/plugins/plugin.h
index 28aedeb..37022c7 100644
--- a/plugins/plugin.h
+++ b/plugins/plugin.h
@@ -4,7 +4,7 @@
#ifdef USE_DB
#include "dbhelper.h"
#endif
-#include "handle.h"
+#include "ws.h"
typedef struct {
char *name;
diff --git a/plugins/ws.c b/plugins/ws.c
index 1f7b833..c5c2a6c 100644
--- a/plugins/ws.c
+++ b/plugins/ws.c
@@ -349,25 +349,7 @@ int request_socket(const char* ip, int port)
}
return sockfd;
}
-int sock_read_buf(int sock, char*buf,int size)
-{
- int i = 0;
- char c = '\0';
- int n;
- while ((i < size - 1) && (c != '\n'))
- {
- n = recv(sock, &c, 1, 0);
- if (n > 0)
- {
- buf[i] = c;
- i++;
- }
- else
- c = '\n';
- }
- buf[i] = '\0';
- return i;
-}
+
//TODO: The ping request
int ws_open_hand_shake(const char* host, int port, const char* resource)
{
@@ -390,7 +372,7 @@ int ws_open_hand_shake(const char* host, int port, const char* resource)
return -1;
}
// now verify if server accept the socket
- size = sock_read_buf(sock,buff,MAX_BUFF);
+ size = read_buf(sock,buff,MAX_BUFF);
char* token;
int done = 0;
while (size > 0 && strcmp("\r\n",buff))
@@ -418,7 +400,7 @@ int ws_open_hand_shake(const char* host, int port, const char* resource)
}
}
//if(line) free(line);
- size = sock_read_buf(sock,buff,MAX_BUFF);
+ size = read_buf(sock,buff,MAX_BUFF);
}
if(done) return sock;
//printf("No server key found \n");
diff --git a/plugins/ws.h b/plugins/ws.h
index 2db6ad3..aff4490 100644
--- a/plugins/ws.h
+++ b/plugins/ws.h
@@ -13,7 +13,7 @@
#include