Esempio n. 1
0
void requestHandler(void *s)
{
	SOCKET sock = *((SOCKET*)s);
	char recvBuf[MAX_HEADER_SIZE + 8];

	char pool[512];
	static const char *skipBrake = "  \n\n:\n\n";
	ExHttp httpInfo;

	++ExContext.threadCnt;

	httpInfo.sock = sock;
	ex_mpool_init(&httpInfo.mp, pool, sizeof(pool));
	do {
		if (ExContext.quitFlag == 1)
			break;

		httpInfo.recvLen = ex_read_head(sock, recvBuf, MAX_HEADER_SIZE);
		if (httpInfo.recvLen <= 0)
			break;

		httpInfo.curPos = recvBuf;
		recvBuf[httpInfo.recvLen] = '\0';
		strcat(recvBuf + httpInfo.recvLen , skipBrake);

		/* if method is not implemented */
		if (checkmethod(&httpInfo) < 0) {
			DBG("len: %d %s\n", httpInfo.method);
			ex_error_reply(&httpInfo, 501);
			break;
		}
		if (parseURL(&httpInfo) < 0) {
			ex_error_reply(&httpInfo, 400);
			errorLog(&httpInfo, "parseURL error");
			break;
		}
		/* if parse head error */
		if (parseHeader(&httpInfo) < 0) {
			ex_error_reply(&httpInfo, 400);
				/* bad Request */
			errorLog(&httpInfo, "parse head error");
			clearHttp(&httpInfo);
			break;
		}
		/* if reply error */
		if (replyHandler(&httpInfo) < 0) {
			break;
		}
	} while (1);

	closesocket(sock);
	--ExContext.threadCnt;
}
Esempio n. 2
0
void requestHandler(void * s)
{
  SOCKET sock = ( SOCKET ) s ;
  char recvBuf[MAX_HEADER_SIZE] ;
  char pool[512] ;
  /*
    int recvlen ;
   */
  ExHttp httpInfo ;
  ++ExContext.threadCnt ;
  httpInfo.sock = sock ;

  ex_mpool_init( &httpInfo.mp, pool, sizeof (pool) ) ;
  do {
    if ( ExContext.quitFlag ) break ;

    httpInfo.recvLen = ex_read_head( sock, recvBuf, MAX_HEADER_SIZE ) ;

    if ( httpInfo.recvLen<=0 ) break ;

    httpInfo.curPos = recvBuf ;
    recvBuf[httpInfo.recvLen] = '\0' ;
    printf( "%s", recvBuf ) ;
    // strcat(recvBuf + httpInfo.recvLen , skipBrake);
    /* if method is not implemented */
    if ( checkmethod( &httpInfo )<0 ) {
      DBG( "len: %s", httpInfo.method ) ;
      ex_error_reply( &httpInfo, 400 ) ;
      errorLog( &httpInfo, "parseURL error" ) ;
      break ;
      // ex_error_reply( &httpInfo, 501 ) ;
      // break ;
    }

    if ( parseURL( &httpInfo )<0 ) {
      DBG( "url: %s protocol %s", httpInfo.url, httpInfo.protocol ) ;
      ex_error_reply( &httpInfo, 400 ) ;
      errorLog( &httpInfo, "parseURL error" ) ;
      break ;
    }

    if ( strcmp( httpInfo.protocol, "HTTP/1.1" )!=0 ) {
      ex_error_reply( &httpInfo, 505 ) ;
      errorLog( &httpInfo, "HTTP Version Not Supported" ) ;
      break ;
    }


    /* if parse head error */
    if ( parseHeader( &httpInfo )<0 ) {
      ex_error_reply( &httpInfo, 400 ) ;
      /* bad Request */
      errorLog( &httpInfo, "parse head error" ) ;
      clearHttp( &httpInfo ) ;
      break ;
    }

    if ( strcmp( httpInfo.url, "/index.jsp" )==0 ) {
      ex_error_reply( &httpInfo, 301 ) ;
      const char * method = httpInfo.method ;
      char * message = "Location: http://127.0.0.1/index.html" ;
      size_t size = strlen( message ) ;
      if ( *method=='G'|| *method=='H' ) {
        ex_sock_nwrite( httpInfo.sock, message, size ) ;
      }
      errorLog( &httpInfo, "Moved Permanently" ) ;
      break ;
    }

    /* if reply error */
    if ( replyHandler( &httpInfo )<0 ) {
      break ;
    }

  } while ( 1 ) ;

  closesocket( sock ) ;
  --ExContext.threadCnt ;
}