Exemplo 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;
}
Exemplo n.º 2
0
int ex_init()
{
	int i = -1;
	static char pool[2048];
	static ex_mpool mpool;

	ex_mpool_init(&mpool, pool, sizeof(pool));
	ex_hash_init(&ExContext.mimeMap, &mpool, 97);
	ex_hash_init(&ExContext.pageMap, &mpool, 97);
	ExContext.pageMap.hashcmp = (void *) ex_hashcasecmp_str;
	ExContext.mimeMap.hashcmp = (void *) ex_hashcasecmp_str;
	/* add MIME type map */
	while (mmt[++i].ext != NULL) {
		ex_hash_add(&ExContext.mimeMap, mmt[i].ext, mmt[i].type);
	}

	ExContext.quitFlag = 0;
	ExContext.threadCnt = 0;

	cgi_init();
	return start_thread((void *) ex_http_start, NULL);
}
Exemplo n.º 3
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 ;
}