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

@ -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;
}

View File

@ -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

View File

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

View File

@ -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");

View File

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