📄 protocol.c
字号:
char *ptr; int clock;{ struct tm *timePtr; if (clock == 0) clock = time(NULL); timePtr = gmtime((time_t*)&clock); strftime(ptr, HTTP_TIME_STRING_LEN,"%a, %d %b %Y %T GMT",timePtr);}void _httpd_sendHeaders(server, contentLength, modTime) httpd *server; int contentLength, modTime;{ char tmpBuf[80], timeBuf[HTTP_TIME_STRING_LEN]; if(server->response.headersSent) return; server->response.headersSent = 1; _httpd_net_write(server->clientSock, "HTTP/1.0 ", 9); _httpd_net_write(server->clientSock, server->response.response, strlen(server->response.response)); _httpd_net_write(server->clientSock, server->response.headers, strlen(server->response.headers)); _httpd_formatTimeString(server, timeBuf, 0); _httpd_net_write(server->clientSock,"Date: ", 6); _httpd_net_write(server->clientSock, timeBuf, strlen(timeBuf)); _httpd_net_write(server->clientSock, "\n", 1); _httpd_net_write(server->clientSock, "Connection: close\n", 18); _httpd_net_write(server->clientSock, "Content-Type: ", 14); _httpd_net_write(server->clientSock, server->response.contentType, strlen(server->response.contentType)); _httpd_net_write(server->clientSock, "\n", 1); if (contentLength > 0) { _httpd_net_write(server->clientSock, "Content-Length: ", 16); snprintf(tmpBuf, sizeof(tmpBuf), "%d", contentLength); _httpd_net_write(server->clientSock, tmpBuf, strlen(tmpBuf)); _httpd_net_write(server->clientSock, "\n", 1); _httpd_formatTimeString(server, timeBuf, modTime); _httpd_net_write(server->clientSock, "Last-Modified: ", 15); _httpd_net_write(server->clientSock, timeBuf, strlen(timeBuf)); _httpd_net_write(server->clientSock, "\n", 1); } _httpd_net_write(server->clientSock, "\n", 1);}httpDir *_httpd_findContentDir(server, dir, createFlag) httpd *server; char *dir; int createFlag;{ char buffer[HTTP_MAX_URL], *curDir; httpDir *curItem, *curChild; strncpy(buffer, dir, HTTP_MAX_URL); curItem = server->content; curDir = strtok(buffer,"/"); while(curDir) { curChild = curItem->children; while(curChild) { if (strcmp(curChild->name, curDir) == 0) break; curChild = curChild->next; } if (curChild == NULL) { if (createFlag == HTTP_TRUE) { curChild = malloc(sizeof(httpDir)); bzero(curChild, sizeof(httpDir)); curChild->name = strdup(curDir); curChild->next = curItem->children; curItem->children = curChild; } else { return(NULL); } } curItem = curChild; curDir = strtok(NULL,"/"); } return(curItem);}httpContent *_httpd_findContentEntry(server, dir, entryName) httpd *server; httpDir *dir; char *entryName;{ httpContent *curEntry; curEntry = dir->entries; while(curEntry) { if (curEntry->type == HTTP_WILDCARD || curEntry->type ==HTTP_C_WILDCARD) break; if (*entryName == 0 && curEntry->indexFlag) break; if (strcmp(curEntry->name, entryName) == 0) break; curEntry = curEntry->next; } if (curEntry) server->response.content = curEntry; return(curEntry);}void _httpd_send304(server) httpd *server;{ httpdSetResponse(server, "304 Not Modified\n"); _httpd_sendHeaders(server,0,0);}void _httpd_send403(server) httpd *server;{ httpdSetResponse(server, "403 Permission Denied\n"); _httpd_sendHeaders(server,0,0); _httpd_sendText(server, "<HTML><HEAD><TITLE>403 Permission Denied</TITLE></HEAD>\n"); _httpd_sendText(server, "<BODY><H1>Access to the request URL was denied!</H1>\n");}void _httpd_send404(server) httpd *server;{ char msg[HTTP_MAX_URL]; snprintf(msg, HTTP_MAX_URL, "File does not exist: %s", server->request.path); _httpd_writeErrorLog(server,LEVEL_ERROR, msg); httpdSetResponse(server, "404 Not Found\n"); _httpd_sendHeaders(server,0,0); _httpd_sendText(server, "<HTML><HEAD><TITLE>404 Not Found</TITLE></HEAD>\n"); _httpd_sendText(server, "<BODY><H1>The request URL was not found!</H1>\n"); _httpd_sendText(server, "</BODY></HTML>\n");}void _httpd_catFile(server, path) httpd *server; char *path;{ int fd, len; char buf[HTTP_MAX_LEN]; fd = open(path,O_RDONLY); if (fd < 0) return; len = read(fd, buf, HTTP_MAX_LEN); while(len > 0) { server->response.responseLength += len; _httpd_net_write(server->clientSock, buf, len); len = read(fd, buf, HTTP_MAX_LEN); } close(fd);}void _httpd_sendStatic(server, data) httpd *server; char *data;{ if (_httpd_checkLastModified(server,server->startTime) == 0) { _httpd_send304(server); } _httpd_sendHeaders(server, server->startTime, strlen(data)); httpdOutput(server, data);}void _httpd_sendFile(server, path) httpd *server; char *path;{ char *suffix; struct stat sbuf; suffix = rindex(path, '.'); if (suffix != NULL) { if (strcasecmp(suffix,".gif") == 0) strcpy(server->response.contentType,"image/gif"); if (strcasecmp(suffix,".jpg") == 0) strcpy(server->response.contentType,"image/jpeg"); if (strcasecmp(suffix,".xbm") == 0) strcpy(server->response.contentType,"image/xbm"); if (strcasecmp(suffix,".png") == 0) strcpy(server->response.contentType,"image/png"); } if (stat(path, &sbuf) < 0) { _httpd_send404(server); return; } if (_httpd_checkLastModified(server,sbuf.st_mtime) == 0) { _httpd_send304(server); } else { _httpd_sendHeaders(server, sbuf.st_size, sbuf.st_mtime); _httpd_catFile(server, path); }}int _httpd_sendDirectoryEntry(server, entry, entryName) httpd *server; httpContent *entry; char *entryName;{ char path[HTTP_MAX_URL]; snprintf(path, HTTP_MAX_URL, "%s/%s", entry->path, entryName); _httpd_sendFile(server,path); return(0);}void _httpd_sendText(server, msg) httpd *server; char *msg;{ server->response.responseLength += strlen(msg); _httpd_net_write(server->clientSock,msg,strlen(msg));}int _httpd_checkLastModified(server, modTime) httpd *server; int modTime;{ char timeBuf[HTTP_TIME_STRING_LEN]; _httpd_formatTimeString(server, timeBuf, modTime); if (strcmp(timeBuf, server->request.ifModified) == 0) return(0); return(1);}static unsigned char isAcceptable[96] =/* Overencodes */#define URL_XALPHAS (unsigned char) 1#define URL_XPALPHAS (unsigned char) 2/* Bit 0 xalpha -- see HTFile.h** Bit 1 xpalpha -- as xalpha but with plus.** Bit 2 ... path -- as xpalpha but with /*/ /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ { 7,0,0,0,0,0,0,0,0,0,7,0,0,7,7,7, /* 2x !"#$%&'()*+,-./ */ 7,7,7,7,7,7,7,7,7,7,0,0,0,0,0,0, /* 3x 0123456789:;<=>? */ 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, /* 4x @ABCDEFGHIJKLMNO */ 7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,7, /* 5X PQRSTUVWXYZ[\]^_ */ 0,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, /* 6x `abcdefghijklmno */ 7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,0 }; /* 7X pqrstuvwxyz{\}~ DEL */ #define ACCEPTABLE(a) ( a>=32 && a<128 && ((isAcceptable[a-32]) & mask))static char *hex = "0123456789ABCDEF";char *_httpd_escape(str) char *str;{ unsigned char mask = URL_XPALPHAS; char * p; char * q; char * result; int unacceptable = 0; for(p=str; *p; p++) if (!ACCEPTABLE((unsigned char)*p)) unacceptable +=2; result = (char *) malloc(p-str + unacceptable + 1); bzero(result,(p-str + unacceptable + 1)); if (result == NULL) { return(NULL); } for(q=result, p=str; *p; p++) { unsigned char a = *p; if (!ACCEPTABLE(a)) { *q++ = '%'; /* Means hex commming */ *q++ = hex[a >> 4]; *q++ = hex[a & 15]; } else *q++ = *p; } *q++ = 0; /* Terminate */ return result;}void _httpd_sanitiseUrl(url) char *url;{ char *from, *to, *last; /* ** Remove multiple slashes */ from = to = url; while(*from) { if (*from == '/' && *(from+1) == '/') { from++; continue; } *to = *from; to++; from++; } *to = 0; /* ** Get rid of ./ sequences */ from = to = url; while(*from) { if (*from == '/' && *(from+1) == '.' && *(from+2)=='/') { from += 2; continue; } *to = *from; to++; from++; } *to = 0; /* ** Catch use of /../ sequences and remove them. Must track the ** path structure and remove the previous path element. */ from = to = last = url; while(*from) { if (*from == '/' && *(from+1) == '.' && *(from+2)=='.' && *(from+3)=='/') { to = last; from += 3; continue; } if (*from == '/') { last = to; } *to = *from; to++; from++; } *to = 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -