clear code

This commit is contained in:
Xuan Sang LE 2017-07-29 23:54:43 +02:00
parent 3695dc1eb1
commit 41bce4f5e8
7 changed files with 70 additions and 176 deletions

View File

@ -86,7 +86,7 @@ void accept_request(int client)
} }
else else
{ {
headers(client,mime_type); ctype(client,mime_type);
// if the mime is supported, send the file // if the mime is supported, send the file
serve_file(client, path); serve_file(client, path);
} }
@ -95,25 +95,6 @@ void accept_request(int client)
close(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, "<P>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 /* 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 * Parameters: the client socket descriptor
* FILE pointer for the file to cat */ * 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) void catb(int client, FILE* ptr)
{ {
unsigned char buffer[BUFFLEN]; unsigned char buffer[BUFFLEN];
@ -175,16 +135,11 @@ void cat(int client, FILE *resource)
/**********************************************************************/ /**********************************************************************/
void cannot_execute(int client) void cannot_execute(int client)
{ {
char buf[1024]; set_status(client,500,"Internal Server Error");
__t(client,SERVER_STRING);
sprintf(buf, "HTTP/1.0 500 Internal Server Error\r\n"); __t(client,"Content-Type: text/html");
send(client, buf, strlen(buf), 0); response(client,"");
sprintf(buf, "Content-type: text/html\r\n"); __t(client, "<P>Error prohibited CGI execution.");
send(client, buf, strlen(buf), 0);
sprintf(buf, "\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, "<P>Error prohibited CGI execution.\r\n");
send(client, buf, strlen(buf), 0);
} }
/**********************************************************************/ /**********************************************************************/
@ -243,50 +198,21 @@ int get_line(int sock, char *buf, int size)
return(i); 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. */ /* Give a client a 404 not found status message. */
/**********************************************************************/ /**********************************************************************/
void not_found(int client) void not_found(int client)
{ {
char buf[1024]; set_status(client,404,"NOT FOUND");
__t(client,SERVER_STRING);
sprintf(buf, "HTTP/1.0 404 NOT FOUND\r\n"); __t(client,"Content-Type: text/html");
send(client, buf, strlen(buf), 0); response(client,"");
sprintf(buf, SERVER_STRING); __t(client, "<HTML><TITLE>Not Found</TITLE>");
send(client, buf, strlen(buf), 0); __t(client, "<BODY><P>The server could not fulfill");
sprintf(buf, "Content-Type: text/html\r\n"); __t(client, "your request because the resource specified");
send(client, buf, strlen(buf), 0); __t(client, "is unavailable or nonexistent.");
sprintf(buf, "\r\n"); __t(client, "</BODY></HTML>");
send(client, buf, strlen(buf), 0);
sprintf(buf, "<HTML><TITLE>Not Found</TITLE>\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, "<BODY><P>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, "</BODY></HTML>\r\n");
send(client, buf, strlen(buf), 0);
} }
/**********************************************************************/ /**********************************************************************/
@ -360,24 +286,14 @@ int startup(unsigned *port)
/**********************************************************************/ /**********************************************************************/
void unimplemented(int client) void unimplemented(int client)
{ {
char buf[1024]; set_status(client,501,"Method Not Implemented");
__t(client,SERVER_STRING);
sprintf(buf, "HTTP/1.0 501 Method Not Implemented\r\n"); __t(client,"Content-Type: text/html");
send(client, buf, strlen(buf), 0); response(client,"");
sprintf(buf, SERVER_STRING); __t(client, "<HTML><HEAD><TITLE>Method Not Implemented");
send(client, buf, strlen(buf), 0); __t(client, "</TITLE></HEAD>");
sprintf(buf, "Content-Type: text/html\r\n"); __t(client, "<BODY><P>HTTP request method not supported.");
send(client, buf, strlen(buf), 0); __t(client, "</BODY></HTML>");
sprintf(buf, "\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, "<HTML><HEAD><TITLE>Method Not Implemented\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, "</TITLE></HEAD>\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, "<BODY><P>HTTP request method not supported.\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, "</BODY></HTML>\r\n");
send(client, buf, strlen(buf), 0);
} }
@ -780,46 +696,6 @@ char* json_data_decode(int client,int len)
return query; 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 * Execute a plugin based on the http requeset
* First decode the http request header to find the correct plugin * First decode the http request header to find the correct plugin

View File

@ -17,15 +17,13 @@
#define ISspace(x) isspace((int)(x)) #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 accept_request(int);
void bad_request(int);
void cat(int, FILE *); void cat(int, FILE *);
void cannot_execute(int); void cannot_execute(int);
void error_die(const char *); void error_die(const char *);
int get_line(int, char *, int); int get_line(int, char *, int);
void headers(int, const char *);
void not_found(int); void not_found(int);
void serve_file(int, const char *); void serve_file(int, const char *);
int startup(unsigned *); 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_multi_part_request(int,const char*);
dictionary decode_cookie(const char*); dictionary decode_cookie(const char*);
char* json_data_decode(int,int); 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, int execute_plugin(int client, const char *path,
const char *method, const char *query_string); const char *method, const char *query_string);

View File

@ -214,4 +214,43 @@ void unknow(int client)
int ws_enable(dictionary dic) int ws_enable(dictionary dic)
{ {
return (dic != NULL && R_INT(dic,"__web_socket__") == 1); 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;
} }

View File

@ -9,7 +9,6 @@
#include "dictionary.h" #include "dictionary.h"
#include "list.h" #include "list.h"
#include "ini.h" #include "ini.h"
#include "ws.h"
#define SERVER_NAME "antd" #define SERVER_NAME "antd"
#define IS_POST(method) (strcmp(method,"POST")== 0) #define IS_POST(method) (strcmp(method,"POST")== 0)
@ -45,4 +44,6 @@ void clear_cookie(int, dictionary);
/*Default function for plugin*/ /*Default function for plugin*/
void unknow(int); void unknow(int);
int ws_enable(dictionary); int ws_enable(dictionary);
char* read_line(int sock);
int read_buf(int sock,char* buf,int i);
#endif #endif

View File

@ -4,7 +4,7 @@
#ifdef USE_DB #ifdef USE_DB
#include "dbhelper.h" #include "dbhelper.h"
#endif #endif
#include "handle.h" #include "ws.h"
typedef struct { typedef struct {
char *name; char *name;

View File

@ -349,25 +349,7 @@ int request_socket(const char* ip, int port)
} }
return sockfd; 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 //TODO: The ping request
int ws_open_hand_shake(const char* host, int port, const char* resource) 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; return -1;
} }
// now verify if server accept the socket // now verify if server accept the socket
size = sock_read_buf(sock,buff,MAX_BUFF); size = read_buf(sock,buff,MAX_BUFF);
char* token; char* token;
int done = 0; int done = 0;
while (size > 0 && strcmp("\r\n",buff)) 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); //if(line) free(line);
size = sock_read_buf(sock,buff,MAX_BUFF); size = read_buf(sock,buff,MAX_BUFF);
} }
if(done) return sock; if(done) return sock;
//printf("No server key found \n"); //printf("No server key found \n");

View File

@ -13,7 +13,7 @@
#include <sys/time.h> #include <sys/time.h>
#include "utils.h" #include "utils.h"
#include "handle.h"
#define CONN_TIME_OUT_S 3 #define CONN_TIME_OUT_S 3
#define BITV(v,i) ((v & (1 << i)) >> i) #define BITV(v,i) ((v & (1 << i)) >> i)
#define WS_TEXT 0x1 #define WS_TEXT 0x1