Example #1
0
char getChar (int fd)
{
  int n;
  char c;
  char *info;

  n = read (fd, &c, 1);
  switch (n){
  case -1:
    info = "fail to read the socket\n";
    write (1, info, strlen (info));
    Http_print (fd, http400);
    close (fd);
    exit (0);
    break;
  case 0: // socket is blocked
    info = "read 0 char\n";
    write (1, info, strlen (info));
    Http_print (fd, http400);
    close (fd);
    exit (0);
    break;
  case 1:
    break;
  default:
    info = "server bug\n";
    write (1, info, strlen (info));
    Http_print (fd, http400);
    close (fd);
    exit (0);
    break;
  }
  return c;
}
Example #2
0
void parseError(int fd)
{
  //eatAllChars(fd);
  Http_print (fd, http400);
  close (fd);
  exit (0);
}
Example #3
0
void *Parse_parse (int fd, int reqOnly)
{
  http400 = generate400();
  //getToken(fd, 1);    modify
  
  ReqLine_t reqline;
  Http_t http = 0;
  if(reqOnly){
    reqline = Parse_reqLine (fd);
    return reqline;
  }
  
  parseHeaders(fd);
  if (token.kind!=TOKEN_CRLF)
    parseError(fd);
  
  int num = parseBody(fd);  
  
  reqline = gReqline;
  
  http = Http_new (HTTP_KIND_REQUEST
		   , reqline
		   , 0
		   , 0
		   , 0);
  
  if (DEBUG)
    Http_print (1, http);
  return http;
}
Example #4
0
Http_t Parse_parse (int fd)
{
  http400 = generate400();
  getToken(fd, 1);

  ReqLine_t reqline;
  Http_t http = 0;

  reqline = parseRequestLine (fd);
  parseHeaders(fd);
  if (token.kind!=TOKEN_CRLF)
    parseError(fd);
  parseBody(fd);
  http = Http_new (HTTP_KIND_REQUEST
		   , reqline
		   , 0
		   , 0
		   , 0);

  if (DEBUG)
    Http_print (1, http);
  return http;
}
Example #5
0
void getToken (int fd, int sepBySpace)
{
  i = 0;
  char s[1024];
  gfd = fd;
  switch (ahead){
  case A_NONE:
    c = getChar (gfd);
    break;
  case A_SPACE:
    ahead = A_NONE;
    Token_new(token, TOKEN_SPACE, 0);
    return;
  case A_CRLF:
    ahead = A_NONE;
    Token_new(token, TOKEN_CRLF, 0);
    return;
  default:{
    char *info = "server bug";
    write (1, info, strlen (info));
    Http_print (gfd, http400);
    close (gfd);
    exit (0);
    return;
  }
  }

  while (1){
    switch (c){
    case ' ':
      if (sepBySpace){
	if (i){
	  char *p;
	  int kind;

	  // remember the ' '
	  ahead = A_SPACE;
	  s[i] = '\0';
	  p = malloc (strlen(s)+1);
	  strcpy (p, s);
	  kind = Token_getKeyWord (p);
	  if (kind>=0){

	    Token_new (token, kind, 0);
	    return;
	  }
	  Token_new (token, TOKEN_STR, p);
	  return;
	}
	Token_new(token, TOKEN_SPACE, 0);
	return;
      }
      s[i++] = c;
      break;
    case '\r':{
      char c2;

      c2 = getChar (gfd);
      if (c2=='\n'){
	if (i){
	  char *p;
	  int kind;
	  // remember the ' '
	  ahead = A_CRLF;
	  s[i] = '\0';
	  p = malloc (strlen(s)+1);
	  strcpy (p, s);
	  kind = Token_getKeyWord (p);
	  if (kind>=0){
	    Token_new (token, kind, 0);
	    return;
	  }
	  Token_new (token, TOKEN_STR, p);
	  return;
	}
	Token_new(token, TOKEN_CRLF, 0);
	return;
      }
      s[i++] = c;
      s[i++] = c2;
      break;
    }
    default:
      s[i++] = c;
      break;
    }
    c = getChar (gfd);
  }
  return;
}