예제 #1
0
void parse_request(request_b *rb) {
  int is_static;
  struct stat sbuf;
  char line[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE];
  char filename[MAXLINE], cgiargs[MAXLINE];
  int is_head = 0;

  if (verbose) {
    printf("the rb->buf start and end:%zu %zu\n", rb->pos, rb->last);
  }
  getlinefrombuf(rb, line);
  sscanf(line, "%s %s %s", method, uri, version);
  if (verbose) {
    printf("the firstline of request\n%s", line);
  }

  if (!strcasecmp(method, "GET")) {  //
    read_requesthdrs(rb);
    is_static = parse_uri(uri, filename, cgiargs);
  } else if (!strcasecmp(method, "HEAD")) {
    read_requesthdrs(rb);
    is_static = parse_uri(uri, filename, cgiargs);
    is_head = 1;
  } else if (!strcasecmp(method, "POST")) {
    parse_post_request(rb, cgiargs, uri, filename);
    is_static = 0;  // always dynamic follow by parse_uri
    printf("cgiargs parameter is %s\n", cgiargs);
  } else {
    clienterror(rb->fd, method, "501", "Not Implemented",
                "Tiny does not implement this method");
    return;
  }

  if (stat(filename, &sbuf) < 0) {
    clienterror(rb->fd, filename, "404", "Not found",
                "Tiny couldn't find this file");
    return;
  }

  if (is_static) { /* Serve static content */
    if (!(S_ISREG(sbuf.st_mode)) || !(S_IRUSR & sbuf.st_mode)) {
      clienterror(rb->fd, filename, "403", "Forbidden",
                  "Tiny couldn't read the file");
      return;
    }
    serve_static(rb->fd, filename, sbuf.st_size, is_head);
  } else { /* Serve dynamic content */
    if (!(S_ISREG(sbuf.st_mode)) ||
        !(S_IXUSR & sbuf.st_mode)) {  // line:netp:doit:executable
      clienterror(rb->fd, filename, "403", "Forbidden",
                  "Tiny couldn't run the CGI program");
      return;
    }
    serve_dynamic(rb->fd, filename, cgiargs,
                  is_head);  // line:netp:doit:servedynamic
  }
}
예제 #2
0
coo_entry_t* get_rating_from_http(const char *str_request, const char *str_regex)
{

	char* header_fields = parse_post_request(str_request, str_regex);
	size_t r;
	if (header_fields==NULL)
	{
		return NULL;
	}
	coo_entry_t* rating= malloc(sizeof(coo_entry_t));		
	if(sscanf(header_fields,"user=%u&item=%u&rating=%d",&rating->row_i,&rating->column_j,&r)!=3)
	{
		free(rating);
		return NULL;
	}
	rating->value = (float) r;
	return rating;
}