Welcome To VxHacks, Inc.\n");
vhttpWriteSock(sock, "
\n");
vhttpWriteSock(sock, "
\n");
vhttpWriteSock(sock, "
\n"); vhttpWriteSock(sock, "
- \n");
sprintf(my_addr, "%s", inet_ntoa(getMyIpAddr()));
for (i = 0, mp = &vtCmds[0]; i < MAX_CMDS; i++, mp++) {
vhttpWriteSock(sock, "
- \n"); sprintf(buf, " %s ", my_addr, HTTP_PORT, i, mp->menu_desc); vhttpWriteSock(sock, buf); } vhttpWriteSock(sock, "
\n"); vhttpWriteSock(sock, "\n"); } int vhttpSendHTML(int sock, char *HTML) { int inFd; int status; char *filename; inFd = open (HTML, O_RDONLY, 0); if (inFd < OK) { errorMsg("can't open HTML file"); return (ERROR); } vhttpWriteSock(sock, "HTTP/1.0 200 OK\nMIME-Version: 1.0\nContent-Type: "); vhttpWriteSock(sock, "text/html\n\n"); status = copyStreams (inFd, sock); return (status); } int vhttpSendGIF(int sock, char *GIF) { int inFd; int status; char *filename; inFd = open (GIF, O_RDONLY, 0); if (inFd < OK) { errorMsg("can't open GIF file"); return (ERROR); } vhttpWriteSock(sock, "HTTP/1.0 200 OK\nMIME-Version: 1.0\nContent-Type: "); vhttpWriteSock(sock, "image/gif\n\n"); status = copyStreams (inFd, sock); return (status); } int vhttpSendJPEG(int sock, char *JPEG) { int inFd; int status; char *filename; inFd = open (JPEG, O_RDONLY, 0); if (inFd < OK) { errorMsg("can't open JPEG file"); return (ERROR); } vhttpWriteSock(sock, "HTTP/1.0 200 OK\nMIME-Version: 1.0\nContent-Type: "); vhttpWriteSock(sock, "image/jpeg\n\n"); status = copyStreams (inFd, sock); return (status); } int vhttpProcessURL(int sock, char *URL) { int num; int result; char *suffix; #ifdef DEBUG printf("URL requested <%s>\n", URL); #endif if (strcmp(URL, "/" ) == 0) vhttpSendMenu(sock); else { suffix = strrchr(URL, '.'); if (suffix != 0) { if (strcmp(suffix,".html") == 0) vhttpSendHTML(sock,++URL); else if (strcmp(suffix, ".gif") == 0) vhttpSendGIF(sock,++URL); else if (strcmp(suffix, ".jpeg") == 0) vhttpSendJPEG(sock,++URL); else { vhttpSendMenu(sock); vhttpSendErr(sock, 666, "Unknown URL"); vhttpSendErr(sock, 666, URL); } } else { URL[0] = ' '; /* wipe out leading slash char */ num = atoi(URL); if (num >= 0 && num < MAX_CMDS) { vhttpSendMenu(sock); result = (*vtCmds[num].menu_func)(); if (result == OK) vhttpSendErr(sock, 205, "Command success"); else vhttpSendErr(sock, 905, "Command Failed"); } else { vhttpSendErr(sock, 505, "Invalid menu item URL"); return ERROR; } } } return OK; } int vhttpProcess(int sock, struct sockaddr_in *sinptr) { fd_set read_fdset; fd_set write_fdset; fd_set except_fdset; int finishedHeader=0; char buffer[1024]; char method[20]; char URL[200]; char proto[20]; for (;;) { FD_SET(sock, &read_fdset); FD_SET(sock, &write_fdset); FD_SET(sock, &except_fdset); if (select(64, &read_fdset, &write_fdset, &except_fdset, 0) < 0) continue; if (FD_ISSET(sock, &read_fdset)) { int result; result = vhttpReadInputLine(sock, buffer); if (result < 0) return -1; if (result == 0) { finishedHeader=1; return 0; } if (!finishedHeader) { sscanf(buffer, "%s %s %s", method, URL, proto); #ifdef DEBUG printf("method <%s> URL <%s> proto <%s>\n", method, URL, proto); #endif if (strcmp("HTTP/1.0", proto)) { vhttpSendErr(sock, 501, "Invalid Protocol. Expecting HTTP/1.0"); close(sock); return -1; } if (strcmp(method, "GET") != 0 && strcmp(method, "PUT") != 0) { vhttpSendErr(sock, 501, "Invalid Method. Expecting GET or PUT."); close(sock); return -1; } if (vhttpProcessURL(sock, URL) != OK) { vhttpSendErr(sock, 404, "Invalid URL"); close(sock); return -1; } close(sock); finishedHeader=1; return 0; } } else if (FD_ISSET(sock, &write_fdset)) { ; } else if (FD_ISSET(sock, &except_fdset)) { errorMsg("exception fd set"); } #if 0 taskDelay(2 * 60); #endif } } int vhttpMainLoop(int sock, struct sockaddr_in *sinptr) { int client; char name[25]; static int procNum = 1; int i; int size; for (;;) { WIND_TCB *tcb; size = sizeof(*sinptr); client = accept(sock, (struct sockaddr *)sinptr, &size); if (client < 0) { errorMsg("accept"); return -1; } infoMsg("vhttp: accepted a new client\n"); sprintf(name, "vhttp%d", procNum++); tcb = taskSpawn(name, 101, 0, 1024 * 4, vhttpProcess, client, sinptr, 0, 0, 0, 0, 0, 0, 0, 0); tcb->spare4 = client; for (i = 0; i < MAX_SESSIONS; i++) { if (vhttp_tasks[i] == 0) break; } if (i < MAX_SESSIONS) vhttp_tasks[i] = tcb; else { vhttpSendErr(sock, 501, "Too many sessions"); close(client); return -1; } } return OK; } int vhttpDelete(WIND_TCB *tcb) { int i; if (tcb == vhttpd_task) close(tcb->spare4); /* delete server socket */ for (i = 0; i < MAX_SESSIONS; i++) { if (tcb == vhttp_tasks[i]) { close(tcb->spare4); vhttp_tasks[i] = 0; } } } int vhttpStart() { int sock; struct sockaddr_in sin; struct sockaddr_in *sinptr = &sin; int true = 1; WIND_TCB *tcb; int i; sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { errorMsg("socket"); return -1; } if((setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&true, sizeof(true))) == -1) { errorMsg("setsockopt"); return -1; } bzero(sinptr, sizeof(*sinptr)); sinptr->sin_family = AF_INET; sinptr->sin_port = htons(HTTP_PORT); sinptr->sin_addr.s_addr = htonl(INADDR_ANY); if (bind(sock, (struct sockaddr *) sinptr, sizeof(*sinptr)) < 0) { errorMsg("bind"); return -1; } if (listen (sock, 5) < 0) { errorMsg("listen"); return -1; } for (i = 0; i < MAX_SESSIONS; i++) vhttp_tasks[i] = 0; tcb = taskSpawn("vHttpD", 100, 0, 2048, vhttpMainLoop, sock, sinptr, 0, 0, 0, 0, 0, 0, 0, 0); tcb->spare4 = sock; vhttpd_task = tcb; taskDeleteHookAdd(vhttpDelete); return OK; } 需要做一些简单修改,就可以在vxworks上run了,只是例子,是用来学习的。