static NSP_FUNCTION(neslib_io_gets) { obj_t *cobj1 = nsp_getobj(N, &N->l, "1"); #if !defined(WIN32)&&!defined(__TURBOC__) struct sigaction sa; int err = 0; #endif char buf[1024]; char *ret; int t = 0; if (nsp_isnum(cobj1)) { t = (int)cobj1->val->d.num; } flush(N); #if !defined(WIN32)&&!defined(__TURBOC__) memset(&sa, 0, sizeof(sa)); sa.sa_handler = timeout; sigaction(SIGALRM, &sa, NULL); alarm(t); #endif ret = fgets(buf, sizeof(buf) - 1, stdin); #if !defined(WIN32)&&!defined(__TURBOC__) err = errno; alarm(0); if (ret == NULL) { switch (err) { case EINTR: printf("timed out\r\n"); return -2; default: return -1; } } #else if (ret == NULL) { printf("fgets() error\r\n"); } #endif striprn(buf); nsp_setstr(N, &N->r, "", buf, -1); return 0; }
int read_header(int sid) { char line[2048]; char *pTemp; time_t x; strncpy(conn[sid].dat->in_RemoteAddr, inet_ntoa(conn[sid].ClientAddr.sin_addr), sizeof(conn[sid].dat->in_RemoteAddr)-1); x=time((time_t*)0); do { memset(line, 0, sizeof(line)); sgets(line, sizeof(line)-1, conn[sid].socket); striprn(line); } while ((strlen(line)==0)&&((time((time_t)0)-x)<30)); if ((strlen(line)==0)&&((time((time_t)0)-x)>=30)) { #ifdef DEBUG logdata("\n[[[ KILLING IDLE KEEPALIVE ]]]\n"); #endif closeconnect(sid, 1); } #ifdef DEBUG logdata("\n[[[ STARTING REQUEST ]]]\n"); #endif if (strlen(line)==0) printerror(sid, 400, "Bad Request", "No Request Found."); if (sscanf(line, "%[^ ] %[^ ] %[^ ]", conn[sid].dat->in_RequestMethod, conn[sid].dat->in_RequestURI, conn[sid].dat->in_Protocol)!=3) printerror(sid, 400, "Bad Request", "Can't Parse Request."); pTemp=conn[sid].dat->in_RequestMethod; while (*pTemp) { *pTemp=toupper(*pTemp); pTemp++; }; while (strlen(line)>0) { sgets(line, sizeof(line)-1, conn[sid].socket); while ((line[strlen(line)-1]=='\n')||(line[strlen(line)-1]=='\r')) line[strlen(line)-1]='\0'; if (strncasecmp(line, "Connection: ", 12)==0) strncpy(conn[sid].dat->in_Connection, (char *)&line+12, sizeof(conn[sid].dat->in_Connection)-1); if (strncasecmp(line, "Content-Length: ", 16)==0) { conn[sid].dat->in_ContentLength=atoi((char *)&line+16); if (conn[sid].dat->in_ContentLength<0) { // Negative Content-Length? If so, the client is either broken or malicious. // Thanks to <*****@*****.**> for spotting this one. logerror("ERROR: negative Content-Length of %d provided by client.", conn[sid].dat->in_ContentLength); conn[sid].dat->in_ContentLength=0; } } if (strncasecmp(line, "Cookie: ", 8)==0) strncpy(conn[sid].dat->in_Cookie, (char *)&line+8, sizeof(conn[sid].dat->in_Cookie)-1); if (strncasecmp(line, "Host: ", 6)==0) strncpy(conn[sid].dat->in_Host, (char *)&line+6, sizeof(conn[sid].dat->in_Host)-1); if (strncasecmp(line, "If-Modified-Since: ", 19)==0) strncpy(conn[sid].dat->in_IfModifiedSince, (char *)&line+19, sizeof(conn[sid].dat->in_IfModifiedSince)-1); if (strncasecmp(line, "User-Agent: ", 12)==0) strncpy(conn[sid].dat->in_UserAgent, (char *)&line+12, sizeof(conn[sid].dat->in_UserAgent)-1); } if ((strcmp(conn[sid].dat->in_RequestMethod, "GET")!=0)&&(strcmp(conn[sid].dat->in_RequestMethod, "POST")!=0)) { printerror(sid, 501, "Not Implemented", "That method is not implemented."); closeconnect(sid, 1); return -1; } if (strcmp(conn[sid].dat->in_RequestMethod, "POST")==0) { if (conn[sid].dat->in_ContentLength<MAX_POSTSIZE) { ReadPOSTData(sid); } else { // try to print an error : note the inbuffer being full may block us // FIXME: this is causing the children to segfault in win32 printerror(sid, 413, "Bad Request", "Request entity too large."); logerror("%s - Large POST (>%d bytes) disallowed", conn[sid].dat->in_RemoteAddr, MAX_POSTSIZE); closeconnect(sid, 1); return -1; } } if (conn[sid].dat->in_RequestURI[0]!='/') { printerror(sid, 400, "Bad Request", "Bad filename."); } if (strchr(conn[sid].dat->in_RequestURI, '?')!=NULL) { strncpy(conn[sid].dat->in_QueryString, strchr(conn[sid].dat->in_RequestURI, '?')+1, sizeof(conn[sid].dat->in_QueryString)-1); } return 0; }