📄 protocol.c
字号:
/*** Copyright (c) 2002 Hughes Technologies Pty Ltd. All rights** reserved.**** Terms under which this software may be used or copied are** provided in the specific license associated with this product.**** Hughes Technologies disclaims all warranties with regard to this** software, including all implied warranties of merchantability and** fitness, in no event shall Hughes Technologies be liable for any** special, indirect or consequential damages or any damages whatsoever** resulting from loss of use, data or profits, whether in an action of** contract, negligence or other tortious action, arising out of or in** connection with the use or performance of this software.****** $Id: protocol.c,v 1.9 2002/11/25 02:15:51 bambi Exp $***/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#include <sys/types.h>#include <sys/stat.h>#include <time.h>#if defined(_WIN32) #else#include <unistd.h>#include <sys/file.h>#endif#include "config.h"#include "httpd.h"#include "httpd_priv.h"int _httpd_net_read(sock, buf, len) int sock; char *buf; int len;{#if defined(_WIN32) return( recv(sock, buf, len, 0));#else return( read(sock, buf, len));#endif}int _httpd_net_write(sock, buf, len) int sock; char *buf; int len;{#if defined(_WIN32) return( send(sock, buf, len, 0));#else return( write(sock, buf, len));#endif}int _httpd_readChar(server, cp) httpd *server; char *cp;{ if (server->readBufRemain == 0) { bzero(server->readBuf, HTTP_READ_BUF_LEN + 1); server->readBufRemain = _httpd_net_read(server->clientSock, server->readBuf, HTTP_READ_BUF_LEN); if (server->readBufRemain < 1) return(0); server->readBuf[server->readBufRemain] = 0; server->readBufPtr = server->readBuf; } *cp = *server->readBufPtr++; server->readBufRemain--; return(1);}int _httpd_readLine(server, destBuf, len) httpd *server; char *destBuf; int len;{ char curChar, *dst; int count; count = 0; dst = destBuf; while(count < len) { if (_httpd_readChar(server, &curChar) < 1) return(0); if (curChar == '\n') { *dst = 0; return(1); } if (curChar == '\r') { continue; } else { *dst++ = curChar; count++; } } *dst = 0; return(1);}int _httpd_readBuf(server, destBuf, len) httpd *server; char *destBuf; int len;{ char curChar, *dst; int count; count = 0; dst = destBuf; while(count < len) { if (_httpd_readChar(server, &curChar) < 1) return(0); *dst++ = curChar; count++; } return(1);}void _httpd_writeAccessLog(server) httpd *server;{ char dateBuf[30]; struct tm *timePtr; time_t clock; int responseCode; if (server->accessLog == NULL) return; clock = time(NULL); timePtr = localtime(&clock); strftime(dateBuf, 30, "%d/%b/%Y:%T %Z", timePtr); responseCode = atoi(server->response.response); fprintf(server->accessLog, "%s - - [%s] %s \"%s\" %d %d\n", server->clientAddr, dateBuf, httpdRequestMethodName(server), httpdRequestPath(server), responseCode, server->response.responseLength);}void _httpd_writeErrorLog(server, level, message) httpd *server; char *level, *message;{ char dateBuf[30]; struct tm *timePtr; time_t clock; if (server->errorLog == NULL) return; clock = time(NULL); timePtr = localtime(&clock); strftime(dateBuf, 30, "%a %b %d %T %Y", timePtr); if (*server->clientAddr != 0) { fprintf(server->errorLog, "[%s] [%s] [client %s] %s\n", dateBuf, level, server->clientAddr, message); } else { fprintf(server->errorLog, "[%s] [%s] %s\n", dateBuf, level, message); }}int _httpd_decode (bufcoded, bufplain, outbufsize) char * bufcoded; char * bufplain; int outbufsize;{ static char six2pr[64] = { 'A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z', 'a','b','c','d','e','f','g','h','i','j','k','l','m', 'n','o','p','q','r','s','t','u','v','w','x','y','z', '0','1','2','3','4','5','6','7','8','9','+','/' }; static unsigned char pr2six[256]; /* single character decode */# define DEC(c) pr2six[(int)c]# define _DECODE_MAXVAL 63 static int first = 1; int nbytesdecoded, j; register char *bufin = bufcoded; register unsigned char *bufout = bufplain; register int nprbytes; /* ** If this is the first call, initialize the mapping table. ** This code should work even on non-ASCII machines. */ if(first) { first = 0; for(j=0; j<256; j++) pr2six[j] = _DECODE_MAXVAL+1; for(j=0; j<64; j++) pr2six[(int)six2pr[j]] = (unsigned char)j; } /* Strip leading whitespace. */ while(*bufcoded==' ' || *bufcoded == '\t') bufcoded++; /* ** Figure out how many characters are in the input buffer. ** If this would decode into more bytes than would fit into ** the output buffer, adjust the number of input bytes downwards. */ bufin = bufcoded; while(pr2six[(int)*(bufin++)] <= _DECODE_MAXVAL); nprbytes = bufin - bufcoded - 1; nbytesdecoded = ((nprbytes+3)/4) * 3; if(nbytesdecoded > outbufsize) { nprbytes = (outbufsize*4)/3; } bufin = bufcoded; while (nprbytes > 0) { *(bufout++)=(unsigned char)(DEC(*bufin)<<2|DEC(bufin[1])>>4); *(bufout++)=(unsigned char)(DEC(bufin[1])<<4|DEC(bufin[2])>>2); *(bufout++)=(unsigned char)(DEC(bufin[2])<<6|DEC(bufin[3])); bufin += 4; nprbytes -= 4; } if(nprbytes & 03) { if(pr2six[(int)bufin[-2]] > _DECODE_MAXVAL) { nbytesdecoded -= 2; } else { nbytesdecoded -= 1; } } bufplain[nbytesdecoded] = 0; return(nbytesdecoded);}char _httpd_from_hex (c) char c;{ return c >= '0' && c <= '9' ? c - '0' : c >= 'A' && c <= 'F'? c - 'A' + 10 : c - 'a' + 10; /* accept small letters just in case */}char * _httpd_unescape(str) char *str;{ char * p = str; char * q = str; static char blank[] = ""; if (!str) return(blank); while(*p) { if (*p == '%') { p++; if (*p) *q = _httpd_from_hex(*p++) * 16; if (*p) *q = (*q + _httpd_from_hex(*p++)); q++; } else { if (*p == '+') { *q++ = ' '; p++; } else { *q++ = *p++; } } } *q++ = 0; return str;} void _httpd_freeVariables(var) httpVar *var;{ httpVar *curVar, *lastVar; if (var == NULL) return; _httpd_freeVariables(var->nextVariable); var->nextVariable = NULL; curVar = var; while(curVar) { lastVar = curVar; curVar = curVar->nextValue; free(lastVar->name); free(lastVar->value); free(lastVar); } return;}void _httpd_storeData(server, query) httpd *server; char *query;{ char *cp, *cp2, var[50], *val, *tmpVal; if (!query) return; cp = query; cp2 = var; bzero(var,sizeof(var)); val = NULL; while(*cp) { if (*cp == '=') { cp++; *cp2 = 0; val = cp; continue; } if (*cp == '&') { *cp = 0; tmpVal = _httpd_unescape(val); httpdAddVariable(server, var, tmpVal); cp++; cp2 = var; val = NULL; continue; } if (val) { cp++; } else { *cp2 = *cp++; if (*cp2 == '.') { strcpy(cp2,"_dot_"); cp2 += 5; } else { cp2++; } } } *cp = 0; tmpVal = _httpd_unescape(val); httpdAddVariable(server, var, tmpVal);}void _httpd_formatTimeString(server, ptr, clock) httpd *server;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -