diff options
Diffstat (limited to 'src/HTTP_Common.c')
| -rw-r--r-- | src/HTTP_Common.c | 244 |
1 files changed, 130 insertions, 114 deletions
diff --git a/src/HTTP_Common.c b/src/HTTP_Common.c index 358da91..578d452 100644 --- a/src/HTTP_Common.c +++ b/src/HTTP_Common.c @@ -1,84 +1,87 @@ #include <stdio.h> -#include <string.h> +#include <stdlib.h> +#include <string.h> #include <ctype.h> #include "HTTP_Common.h" #include "HTTP_Message.h" - void http_freeBuf(int thread_seq, char **buf, uint32 *buflen) { - if(*buf!=NULL) + if (*buf != NULL) { - dictator_free(thread_seq,*buf); + dictator_free(thread_seq, *buf); *buf = NULL; *buflen = 0; - } + } return; } -char* double_memchr(const char* before, uint32 before_len, const char* this_data, uint32 this_len, char c) +char *double_memchr(const char *before, uint32 before_len, const char *this_data, uint32 this_len, char c) { /*before only find last two char*/ - if(before_len>=1) + if (before_len >= 1) { - if(*(before+(before_len-1))==c) return (char*)(before+(before_len-1)); + if (*(before + (before_len - 1)) == c) + return (char *)(before + (before_len - 1)); } - if(before_len>=2) + if (before_len >= 2) { - if(*(before+(before_len-2))==c) return (char*)(before+(before_len-2)); + if (*(before + (before_len - 2)) == c) + return (char *)(before + (before_len - 2)); } - return (char*)memchr(this_data,c,this_len); + return (char *)memchr(this_data, c, this_len); } /* char* double_memchr(const char* before, uint32 before_len, const char* this_data, uint32 this_len, char c) { - char*pos=(char*)memchr(before,c,before_len); - if(pos!=NULL) - { - return pos; - } - pos=(char*)memchr(this_data,c,this_len); - return pos; + char*pos=(char*)memchr(before,c,before_len); + if(pos!=NULL) + { + return pos; + } + pos=(char*)memchr(this_data,c,this_len); + return pos; } */ -char* double_memread(const char*before,uint32 before_len,const char* this_data,uint32 this_len,const char*pos,uint32 offset) +char *double_memread(const char *before, uint32 before_len, const char *this_data, uint32 this_len, const char *pos, uint32 offset) { - const char* v_target=pos+offset; - if((pos>=before&&pos<before+before_len)||(pos>=this_data&&pos<this_data+this_len)) + const char *v_target = pos + offset; + if ((pos >= before && pos < before + before_len) || (pos >= this_data && pos < this_data + this_len)) { - if(before<=v_target&&v_target<before+before_len) + if (before <= v_target && v_target < before + before_len) { - return (char*)v_target; - } - if(this_data<=v_target&&v_target<this_data+this_len) + return (char *)v_target; + } + if (this_data <= v_target && v_target < this_data + this_len) { - return (char*)v_target; + return (char *)v_target; } - if(this_data<=(this_data+offset-(before+before_len-pos))&&(this_data+offset-(before+before_len-pos))<this_data+this_len) + if (this_data <= (this_data + offset - (before + before_len - pos)) && (this_data + offset - (before + before_len - pos)) < this_data + this_len) { - return (char*)(this_data+offset-(before+before_len-pos)); - } - } + return (char *)(this_data + offset - (before + before_len - pos)); + } + } return NULL; } -uint32 double_mem_offset(const char*before,uint32 before_len,const char* this_data,uint32 this_len,const char* p) +uint32 double_mem_offset(const char *before, uint32 before_len, const char *this_data, uint32 this_len, const char *p) { - if(p>=this_data&&p<this_data+this_len) + if (p >= this_data && p < this_data + this_len) { - return before_len+(p-this_data); + return before_len + (p - this_data); } - if(p>=before&&p<before+before_len) + if (p >= before && p < before + before_len) { - return p-before; - } + return p - before; + } return -1; } char A_to_a(char ch) { - if(ch>='A'&&ch<='Z') return ch+'a'-'A'; + if (ch >= 'A' && ch <= 'Z') + return ch + 'a' - 'A'; return ch; } @@ -87,27 +90,29 @@ char A_to_a(char ch) * offset:����ֵ����һ�����ǿ��У��ո��Ʊ��������ڵ�ǰ���ݵ�ƫ������ * data����ǰ���ݡ� * data_len:��ǰ���ݵij��ȡ� -***********************************************************/ -void http_deleteEmptyRow(uint32 *offset, char* data, uint32 data_len) -{ - if(NULL==data||data_len==0) return; - while(*offset<data_len && (int)*offset>=0) + ***********************************************************/ +void http_deleteEmptyRow(uint32 *offset, char *data, uint32 data_len) +{ + if (NULL == data || data_len == 0) + return; + while (*offset < data_len && (int)*offset >= 0) { - if(HTTP_CR!=*(data+(*offset)) && HTTP_NULL!=*(data+(*offset)) && HTTP_LF!=*(data+(*offset)) && HTTP_SP!=*(data+(*offset)) && HTTP_HT!=*(data+(*offset))) + if (HTTP_CR != *(data + (*offset)) && HTTP_NULL != *(data + (*offset)) && HTTP_LF != *(data + (*offset)) && HTTP_SP != *(data + (*offset)) && HTTP_HT != *(data + (*offset))) break; - if(HTTP_CR==*(data+(*offset)) || HTTP_NULL==*(data+(*offset)) || HTTP_LF==*(data+(*offset)) || HTTP_SP==*(data+(*offset)) || HTTP_HT==*(data+(*offset))) + if (HTTP_CR == *(data + (*offset)) || HTTP_NULL == *(data + (*offset)) || HTTP_LF == *(data + (*offset)) || HTTP_SP == *(data + (*offset)) || HTTP_HT == *(data + (*offset))) (*offset)++; } } -void http_deleteEmptyRow_chunk(uint32 *offset, char* data, uint32 data_len) -{ - if(NULL==data||data_len==0) return; - while(*offset<data_len && (int)*offset>=0) +void http_deleteEmptyRow_chunk(uint32 *offset, char *data, uint32 data_len) +{ + if (NULL == data || data_len == 0) + return; + while (*offset < data_len && (int)*offset >= 0) { - if(HTTP_CR!=*(data+(*offset)) && HTTP_LF!=*(data+(*offset))&& HTTP_SP!=*(data+(*offset)) && HTTP_HT!=*(data+(*offset)) && 0!=*(data+(*offset))) + if (HTTP_CR != *(data + (*offset)) && HTTP_LF != *(data + (*offset)) && HTTP_SP != *(data + (*offset)) && HTTP_HT != *(data + (*offset)) && 0 != *(data + (*offset))) break; - if(HTTP_CR==*(data+(*offset)) || HTTP_LF==*(data+(*offset))|| HTTP_SP==*(data+(*offset)) || HTTP_HT==*(data+(*offset)) || 0==*(data+(*offset))) + if (HTTP_CR == *(data + (*offset)) || HTTP_LF == *(data + (*offset)) || HTTP_SP == *(data + (*offset)) || HTTP_HT == *(data + (*offset)) || 0 == *(data + (*offset))) (*offset)++; } return; @@ -118,40 +123,42 @@ void http_deleteEmptyRow_chunk(uint32 *offset, char* data, uint32 data_len) * offset:����ֵ����һ�����ǿ��У��ո��Ʊ��������ڵ�ǰ���ݵ�ƫ������ * data����ǰ���ݡ� * data_len����ǰ���ݵij��ȡ� -***********************************************************/ -void http_deleteSPHT(uint32 *offset, char* data, uint32 data_len) + ***********************************************************/ +void http_deleteSPHT(uint32 *offset, char *data, uint32 data_len) { - while(*offset<data_len && (int)*offset>=0) + while (*offset < data_len && (int)*offset >= 0) { - if(HTTP_SP!=*(data+(*offset)) && HTTP_HT!=*(data+(*offset))) + if (HTTP_SP != *(data + (*offset)) && HTTP_HT != *(data + (*offset))) break; (*offset)++; } -}/*http_deleteSPHTCRLFAtLast*/ +} /*http_deleteSPHTCRLFAtLast*/ /********************************************************** * ���ܣ�ɾ��data����Ŀո��У��Ʊ����� * del_data_len��ɾ�������ݵij��ȡ� * data����ǰ���ݡ� * data_len����ǰ���ݵij��ȡ� -***********************************************************/ -void http_deleteSPHTCRLFAtLast(uint32 *del_data_len, char* data, uint32 data_len) + ***********************************************************/ +void http_deleteSPHTCRLFAtLast(uint32 *del_data_len, char *data, uint32 data_len) { - if (data == NULL || data_len == 0 || del_data_len == NULL) { - return; - } + if (data == NULL || data_len == 0 || del_data_len == NULL) + { + return; + } - // ��ʼ��Ҫɾ���ij���Ϊ���ݳ��� - *del_data_len = data_len; + // ��ʼ��Ҫɾ���ij���Ϊ���ݳ��� + *del_data_len = data_len; - // ������ĩβ��ǰ���ҷǿհ��ַ��ͻ��з� - while (*del_data_len > 0 && - (data[*del_data_len - 1] == HTTP_CR || - data[*del_data_len - 1] == HTTP_HT || - data[*del_data_len - 1] == HTTP_SP || - data[*del_data_len - 1] == HTTP_LF)) { - (*del_data_len)--; - } + // ������ĩβ��ǰ���ҷǿհ��ַ��ͻ��з� + while (*del_data_len > 0 && + (data[*del_data_len - 1] == HTTP_CR || + data[*del_data_len - 1] == HTTP_HT || + data[*del_data_len - 1] == HTTP_SP || + data[*del_data_len - 1] == HTTP_LF)) + { + (*del_data_len)--; + } } /********************************************************** @@ -159,88 +166,91 @@ void http_deleteSPHTCRLFAtLast(uint32 *del_data_len, char* data, uint32 data_len * res:���ֵ��ͨ���ò����ж��Ƿ����\r\n * all_data:�������ݻ������� **********************************************************/ -char* http_findCRLF(char *all_data, uint32 len) +char *http_findCRLF(char *all_data, uint32 len) { - if(NULL==all_data||0==len) return NULL; - char *s_cr=NULL; - s_cr=(char*)memchr(all_data,'\r',len); + if (NULL == all_data || 0 == len) + return NULL; + char *s_cr = NULL; + s_cr = (char *)memchr(all_data, '\r', len); /*����\r*/ - if(NULL!=s_cr) - { + if (NULL != s_cr) + { return s_cr; } - /*û����\r*/ + /*û����\r*/ return NULL; } -char* http_findCRLF_Chunk(char *all_data, uint32 len, uint32* end_flag_size) +char *http_findCRLF_Chunk(char *all_data, uint32 len, uint32 *end_flag_size) { - if(all_data==NULL || len==0) return NULL; + if (all_data == NULL || len == 0) + return NULL; - char *s_cr=NULL; - char *s_lf=NULL; - char *p_ret=NULL; - uint32 rf = 2; - s_cr=(char*)memchr(all_data, '\r',len); - s_lf=(char*)memmem(all_data, len, (void*)"\0\n", rf); - if(NULL!=s_cr && NULL!=s_lf) + char *s_cr = NULL; + char *s_lf = NULL; + char *p_ret = NULL; + uint32 rf = 2; + s_cr = (char *)memchr(all_data, '\r', len); + s_lf = (char *)memmem(all_data, len, (void *)"\0\n", rf); + if (NULL != s_cr && NULL != s_lf) { p_ret = MIN(s_cr, s_lf); - *end_flag_size = (p_ret == s_cr)? 1:2; + *end_flag_size = (p_ret == s_cr) ? 1 : 2; } - else if(NULL == s_cr && NULL == s_lf) + else if (NULL == s_cr && NULL == s_lf) { } else { - p_ret = (s_lf != NULL)?s_lf:s_cr; - *end_flag_size = (s_lf != NULL)? 2:1; - } + p_ret = (s_lf != NULL) ? s_lf : s_cr; + *end_flag_size = (s_lf != NULL) ? 2 : 1; + } return p_ret; } -void stringA_to_stringa(char* str, uint32 l_str) +void stringA_to_stringa(char *str, uint32 l_str) { - uint16 m =0; - for(m=0;m<l_str;m++) + uint16 m = 0; + for (m = 0; m < l_str; m++) { str[m] = A_to_a(str[m]); } } /* this function is copy from other system. */ -char *memcasemem(const char *strsrc,int len1,const char *substr,int len2) +char *memcasemem(const char *strsrc, int len1, const char *substr, int len2) { - - char *p1,*p2,*pend; + + char *p1, *p2, *pend; char *p; char *substrS; - int i,lenth; + int i, lenth; - if((strsrc==NULL)||substr==NULL) + if ((strsrc == NULL) || substr == NULL) + return NULL; + if (len1 < len2) return NULL; - if(len1<len2) return NULL; - lenth=len2; - substrS=(char *)malloc(sizeof(char)*(lenth+1)); - if(substrS==NULL) + lenth = len2; + substrS = (char *)malloc(sizeof(char) * (lenth + 1)); + if (substrS == NULL) return NULL; - for(i=0;i<lenth;i++) + for (i = 0; i < lenth; i++) { - substrS[i]=tolower(*(substr+i)); + substrS[i] = tolower(*(substr + i)); } - substrS[lenth]=0; + substrS[lenth] = 0; - lenth=len1; - pend =(char *)strsrc + lenth; - for(p1 =(char *) strsrc; p1 != pend;p1++) + lenth = len1; + pend = (char *)strsrc + lenth; + for (p1 = (char *)strsrc; p1 != pend; p1++) { - for(p2 = p1,p = substrS;*p != '\0'&& p2 != pend;p++,p2++) + for (p2 = p1, p = substrS; *p != '\0' && p2 != pend; p++, p2++) { - if(tolower(*p2) != *p) + if (tolower(*p2) != *p) break; } - if(*p == '\0') + if (*p == '\0') { free(substrS); return p1; @@ -249,14 +259,20 @@ char *memcasemem(const char *strsrc,int len1,const char *substr,int len2) free(substrS); return NULL; -} +} +#include <time.h> long long rdtsc(void) { +#if 0 // adapt for x86 and AArch64 long long l; long long h; __asm__ volatile("rdtsc" : "=a"(l), "=d"(h)); return (long long )l | ((long long )h<<32); +#else + struct timespec curtime; + clock_gettime(CLOCK_MONOTONIC, &curtime); + return (long long)curtime.tv_sec * 1000 * 1000 * 1000 + (long long)curtime.tv_nsec; +#endif } - |
