Ejemplo n.º 1
0
/* $begin doit */
void * doit( void * fd_ptr) 
{
    int fd = * (int *)fd_ptr;
    printf("\nFile Desc = %d", fd);
    int is_static;
    struct stat sbuf;
    char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE];
    char filename[MAXLINE], cgiargs[MAXLINE];
    rio_t rio;

    /* Read request line and headers */
    Rio_readinitb(&rio, fd);
    if (!Rio_readlineb(&rio, buf, MAXLINE)) { //line:netp:doit:readrequest {
        pthread_exit(NULL);
        return NULL;
    }
    printf("%s", buf);
    sscanf(buf, "%s %s %s", method, uri, version);       //line:netp:doit:parserequest
    if (strcasecmp(method, "GET")) {                     //line:netp:doit:beginrequesterr
        clienterror(fd, method, "501", "Not Implemented",
                    "Tiny does not implement this method");
        pthread_exit(NULL);
        return NULL;
    }                                                    //line:netp:doit:endrequesterr
    read_requesthdrs(&rio);                              //line:netp:doit:readrequesthdrs

    /* Parse URI from GET request */
    is_static = parse_uri(uri, filename, cgiargs);       //line:netp:doit:staticcheck
    if (stat(filename, &sbuf) < 0) {                     //line:netp:doit:beginnotfound
	clienterror(fd, filename, "404", "Not found",
		    "Tiny couldn't find this file");
    pthread_exit(NULL);
	return NULL;
    }                                                    //line:netp:doit:endnotfound

    if (is_static) { /* Serve static content */          
	if (!(S_ISREG(sbuf.st_mode)) || !(S_IRUSR & sbuf.st_mode)) { //line:netp:doit:readable
	    clienterror(fd, filename, "403", "Forbidden",
			"Tiny couldn't read the file");
        pthread_exit(NULL);
	    return NULL;
	}
	serve_static(fd, filename, sbuf.st_size);        //line:netp:doit:servestatic
    }
    else { /* Serve eynamic content */
	if (!(S_ISREG(sbuf.st_mode)) || !(S_IXUSR & sbuf.st_mode)) { //line:netp:doit:executable
	    clienterror(fd, filename, "403", "Forbidden",
			"Tiny couldn't run the CGI program");
        pthread_exit(NULL);
	    return NULL;
	}
	serve_dynamic(fd, filename, cgiargs);            //line:netp:doit:servedynamic
    }
    Close(fd);
    pthread_exit(NULL);
    return NULL;
}
Ejemplo n.º 2
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
  }
}
Ejemplo n.º 3
0
// 处理HTTP事务
void doit(int fd)
{
    int is_static;
    struct stat sbuf;
    char buf[LINE_MAX], method[LINE_MAX], uri[LINE_MAX], version[LINE_MAX];
    char filename[LINE_MAX], cgiargs[LINE_MAX];

    // 读入请求行
    readline(fd, buf, sizeof(buf));
    sscanf(buf, "%s %s %s", method, uri, version);

    // 读入请求报头,简单打印到标准输出
    while (readline(fd, buf, sizeof(buf)))
    {
        printf("%s", buf);
        if (strcmp(buf, "\r\n") == 0)
            break;
    }

    if (strcmp(method, "GET"))
    {
        client_error(fd, method, "501", "Impelementd", "minihttp does not impelement this method");
        return;
    }

    is_static = parse_uri(uri, filename, cgiargs);
    if (stat(filename, &sbuf) < 0)
    {
        client_error(fd, filename, "404", "Not found", "miniftp couldn't find this file");
        return;
    }

    if (is_static) /* Serve static content */
    {
        if (!(S_ISREG(sbuf.st_mode)) || !(S_IRUSR & sbuf.st_mode))
        {
            client_error(fd, filename, "403", "Forbidden", "minihttp couldn't read the file");
            return;
        }
        serve_static(fd, filename, sbuf.st_size);
    }
    else /* Serve dynamic content */
    {
        if (!(S_ISREG(sbuf.st_mode)) || !(S_IRUSR & sbuf.st_mode))
        {
            client_error(fd, filename, "403", "Forbidden", "minihttp couldn't run the CGI program");
            return;
        } 
        serve_dynamic(fd, filename, cgiargs);
    }

}
Ejemplo n.º 4
0
/** @brief Handles HTTP GET requests.
 *  Reads and parses the request, the uri, and dispatches the appropriate
 *  functions to serve content.
 *  @param conn_fd The connection file descriptor.
 *  @return none.
 */
void handle_request(int conn_fd)
{
	rio_t rio;
	char request[MAXLINE], uri[MAXLINE];
	char file_name[MAXLINE], cgi_args[MAXLINE];
	int is_static;
	struct stat st_buf;

	Rio_readinitb(&rio, conn_fd);

	/* Read first header line of incoming request. */
	if (rio_readlineb(&rio, request, MAXLINE) < 0) {
		dbg_printf("rio_readlineb error\n");
		return;
	}
	dbg_printf("Request: %s", request);

	if (parse_request_header(conn_fd, request, uri) != SUCCESS) {
		return;
	}

	if (read_request_headers(&rio) != SUCCESS) {
		return;
	}

	find_file(uri, file_name, cgi_args, &is_static);

	if (stat(file_name, &st_buf) < 0) {
		dbg_printf("404: File not found: %s\n", file_name);
		send_error_msg(conn_fd, "404", "File not found");
		return;
	}

	/* Handle static content */
	if (is_static) {
		if (!(S_ISREG(st_buf.st_mode)) || !(S_IRUSR & st_buf.st_mode)) {
			dbg_printf("403: Can't read the file: %s\n", file_name);
			send_error_msg(conn_fd, "403", "Can't read the file.");
			return;
		}
		serve_static(conn_fd, file_name, st_buf.st_size);
	}
	/* Handle dynamic content */
	else {
		if (!(S_ISREG(st_buf.st_mode)) || !(S_IXUSR & st_buf.st_mode)) {
			dbg_printf("403: Can't run the CGI program: %s\n", file_name);
			send_error_msg(conn_fd, "403", "Can't run the CGI program.");
	 		return;
		}
		serve_dynamic(conn_fd, file_name, cgi_args);
	}
}
Ejemplo n.º 5
0
/* $begin ServeClient */
void ServeClient(int fd)
{
    int is_static;
    struct stat sbuf;
    char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE];
    char filename[MAXLINE], cgiargs[MAXLINE];
    rio_t rio;

	cgiargs[0] = 0;
    /* Read request line and headers */
    Rio_readinitb(&rio, fd);
    Rio_readlineb(&rio, buf, MAXLINE);
    sscanf(buf, "%s %s %s", method, uri, version);

    if ( strcasecmp(method, "GET") == 0 ) {
		is_static = parse_uri(uri, filename, cgiargs);
    } else if ( strcasecmp(method, "POST") == 0 ) {
    	is_static = parse_uri(uri, filename, cgiargs);
    } else {
    	clienterror(fd, method, "501", "Not Implemented", "Server does not implement this method");
        return;
    }

    if (stat(filename, &sbuf) < 0)
    {
        clienterror(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(fd, filename, "403", "Forbidden",
                        "Tiny couldn't read the file");
            return;
        }
        serve_static(fd, filename, sbuf.st_size);
    }
    else   /* Serve dynamic content */
    {
        if (!(S_ISREG(sbuf.st_mode)) || !(S_IXUSR & sbuf.st_mode))
        {
            clienterror(fd, filename, "403", "Forbidden",
                        "Tiny couldn't run the CGI program");
            return;
        }
        serve_dynamic(fd, filename, cgiargs, &rio);
    }
}
Ejemplo n.º 6
0
void doit(int fd)
{
	int is_static;
	struct stat sbuf;
	char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE];
	char filename[MAXLINE], cgiargs[MAXLINE];
	rio_t rio;

	Rio_readinitb(&rio, fd);
	Rio_readlineb(&rio, buf, MAXLINE);
	sscanf(buf, "%s %s %s", method, uri, version);
	if (strcasecmp(method, "GET"))//仅支持GET方法
	{
		clienterror(fd, method, "501", "Not Implemented",
			"Tiny does not implement this method");
		return;
	}
	read_requesthdrs(&rio);//忽略任何请求报头

	is_static = parse_uri(uri, filename, cgiargs);//URI解析(动态或是静态)
	if (stat(filename, &sbuf) < 0)
	{
		clienterror(fd, filename, "404", "Not found",
			"Tiny couldn't find this file");
		return;
	}

	if (is_static)
	{
		if(!(S_ISREG(sbuf.st_mode)) || !(S_IRUSR & sbuf.st_mode))
		{
			clienterror(fd, filename, "403", "Forbidden",
			"Tiny couldn't read this file");
		     return;
		}
		serve_static(fd, filename, sbuf.st_size);
	}
	else
	{
		if(!(S_ISREG(sbuf.st_mode)) || !(S_IXUSR & sbuf.st_mode))
		{
			clienterror(fd, filename, "403", "Forbidden",
			"Tiny couldn't run the CGI program");
		     return;
		}
		serve_dynamic(fd, filename, cgiargs);
	}
}
Ejemplo n.º 7
0
Archivo: tiny.c Proyecto: akxxsb/tiny
/*
 * doit - 处理http请求
 */
void doit(int fd)
{
    int is_static;
    struct stat sbuf;
    char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE];
    char filename[MAXLINE], cgiargs[MAXLINE];
    rio_t rio;

    /* 读取请求行和报头 */
    Rio_readinitb(&rio, fd);
    if (!Rio_readlineb(&rio, buf, MAXLINE))
        return;
    printf("%s", buf);
    sscanf(buf, "%s %s %s", method, uri, version);
    if (strcasecmp(method, "GET")) {                     //判断是否是GET方法
        clienterror(fd, method, "501", "Not Implemented",
                    "Tiny does not implement this method");
        return;
    }
    read_requesthdrs(&rio);                              //处理报头

    /* 解析uri */
    is_static = parse_uri(uri, filename, cgiargs);
    if (stat(filename, &sbuf) < 0) {
	clienterror(fd, filename, "404", "Not found",
		    "Tiny couldn't find this file");
	return;
    }

    if (is_static) { /* 服务静态内容 */
	if (!(S_ISREG(sbuf.st_mode)) || !(S_IRUSR & sbuf.st_mode)) {
	    clienterror(fd, filename, "403", "Forbidden",
			"Tiny couldn't read the file");
	    return;
	}
	serve_static(fd, filename, sbuf.st_size);
    }
    else { /*服务动态内容 */
	if (!(S_ISREG(sbuf.st_mode)) || !(S_IXUSR & sbuf.st_mode)) {
	    clienterror(fd, filename, "403", "Forbidden",
			"Tiny couldn't run the CGI program");
	    return;
	}
	serve_dynamic(fd, filename, cgiargs);            //line:netp:doit:servedynamic
    }
}
Ejemplo n.º 8
0
Archivo: tiny.c Proyecto: 4179e1/misc
void doit (int fd)
{
	bool is_static;
	struct stat sbuf;
	char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE];
	char filename[MAXLINE], cgiargs[MAXLINE];

	wp_rio_t *rio;

	rio = wp_rio_new (fd);
	wp_rio_readlineb (rio, buf, sizeof (buf));
	sscanf (buf, "%s %s %s", method, uri, version);

	if (strcasecmp (method, "GET") != 0)
	{
		clienterror (fd, method, "501", "not Implemented", "Tiny does not implement this method");
		return;
	}
	read_requesthdrs (rio);

	is_static = parse_uri (uri, filename, cgiargs);
	if (wp_stat(filename, &sbuf) < 0)
	{
		clienterror (fd, filename, "404", "Not found", "Tiny couldn't find this file");
		return;
	}

	if (is_static)
	{
		if (!(S_ISREG(sbuf.st_mode)) || !(S_IRUSR & sbuf.st_mode))
		{
			clienterror (fd, filename, "403", "Forbidden", "Tiny couldn't read the file");
			return;
		}
		serve_static (fd, filename, sbuf.st_size);
	}
	else
	{
		if (!(S_ISREG(sbuf.st_mode)) || !(S_IXUSR & sbuf.st_mode))
		{
			clienterror (fd, filename, "403", "Forbidden", "Tiny couldn't run the CGI program");
			return;
		}
		serve_dynamic (fd, filename, cgiargs);
	}
}
Ejemplo n.º 9
0
/*
 * doit - handle an HTTP transaction
 */
void doit(int connfd) {

    char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE];
    char filename[MAXLINE], cgiargs[MAXLINE];
    rio_t rio;
    int is_static;
    struct stat filestat;

    /* parse request, for Tiny Server, it's typically "GET / HTTP/1.0" */
    Rio_readinitb(&rio, connfd);	/* subsequent read will read from connfd */
    Rio_readlineb(&rio, buf, MAXLINE);	/* read first line of HTTP request */
    sscanf(buf, "%s %s %s", method, uri, version); /* format */

    if (strcasecmp(method, "GET")) { /* Tiny Server can only handle GET method */
	clienterror(connfd, method, "501", "Not Implemented",
		    "Tiny does not support the method ");
	return;
    }
    read_requesthdrs(&rio);

    /* parse uri, put requested file name into `filename`, and put arguments */
    /* into cgiargs if any */
    is_static = parse_uri(uri, filename, cgiargs);
    if (stat(filename, &filestat) < 0) {
	clienterror(connfd, filename, "404", "Not Found",
		    "Tiny couldn't find the file ");
	return;
    }

    if (is_static) {		/* serve static content */
	if (!S_ISREG(filestat.st_mode) || !(S_IRUSR & filestat.st_mode)) {
	    clienterror(connfd, filename, "403", "Forbidden",
			"Tiny couldn't read the file ");
	    return;
	}
	serve_static(connfd, filename, filestat);
    } else {
	if (!S_ISREG(filestat.st_mode) || !(S_IXUSR & filestat.st_mode)) {
	    clienterror(connfd, filename, "403", "Forbidden",
			"Tiny couldn't execute the cgi file ");
	    return;
	}
	serve_dynamic(connfd, filename, cgiargs);
    }
}
Ejemplo n.º 10
0
void doit(int fd) {
    int is_static;
    struct stat sbuf;
    char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE];
    char filename[MAXLINE], cgiargs[MAXLINE];
    rio_t rio;

    // read request line and headers
    Rio_readinitb(&rio, fd);
    Rio_readlineb(&rio, buf, MAXLINE);
    sscanf(buf, "%s %s %s", method, uri, version);
    if (strcasecmp(method, "GET")) {
        clienterror(fd, method, "501", "Not Implemented", "Tiny does not implement this method");
        return;
    }
    Rio_writen(rio.rio_fd, "OK!\n", 4);
    read_requesthdrs(&rio);
    Rio_writen(rio.rio_fd, "OK!\n", 4);
    // parse uri from GET request
    is_static = parse_uri(uri, filename, cgiargs);
    if (stat(filename, &sbuf) < 0) {
        clienterror(fd, filename, "404", "Not found", "Tiny couldn't find the file");
        return;
    }
    if (is_static) {
    // server static content
        if (!(S_ISREG(sbuf.st_mode)) || !(S_IRUSR & sbuf.st_mode)) {
            clienterror(fd, filename, "403", "Forbidden", "Tiny could't read the file");
            return;
        }
        runtimeLogFmt("arrive line : %d", __LINE__);
        serve_static(fd, filename, sbuf.st_size);
    } else {
        // serve dynamic content
        if (!(S_ISREG(sbuf.st_mode)) || !(S_IXUSR & sbuf.st_mode)) {
            clienterror(fd, filename, "403", "Forbidden", "Tiny couldn't run the CGI program");
            return;
            serve_dynamic(fd, filename, cgiargs);
        }
    }
}
Ejemplo n.º 11
0
void doit(int fd)
{
        rio_t rio;
        Rio_readinitb(&rio, fd);
        char *request = read_request(&rio);
        if (request == NULL)
                return;
        write_to_file(request);

        char method[MAXLINE], uri[MAXLINE], version[MAXLINE];
        sscanf(request, "%s %s %s", method, uri, version);
        free(request);
        if (strcasecmp(method, "GET") && strcasecmp(method, "HEAD")) {
                clienterror(fd, method, "501", "Not implemented", "Tiny does not implement this method");
                return;
        }

        char filename[MAXLINE], cgiargs[MAXLINE];
        int is_static = parse_uri(uri, filename, cgiargs);
        struct stat sbuf;
        if (stat(filename, &sbuf) < 0) {
                clienterror(fd, filename, "404", "Not found", "Tiny couldn't read the file");
                return;
        }

        if (is_static) {        /* Serve static content */
                if (!(S_ISREG(sbuf.st_mode)) || !(S_IRUSR & sbuf.st_mode)) {
                        clienterror(fd, filename, "403", "Forbidden", "Tiny couldn't read the filetype");
                        return;
                }
                serve_static(fd, filename, sbuf.st_size, method);
        } else {                  /* Serve dynamic content */
                if (!(S_ISREG(sbuf.st_mode)) || !(S_IXUSR & sbuf.st_mode)) {
                        clienterror(fd, filename, "403", "Forbidden", "Tiny couldn't run the CGI program");
                        return;
                }
                serve_dynamic(fd, filename, cgiargs, method);
        }
}
Ejemplo n.º 12
0
void doit(int fd)
{
        int is_static;
        struct stat sbuf;
        char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE];
        char filename[MAXLINE], cgiargs[MAXLINE];
        rio_t rio;

        /* Read request line and headers, put them into a file */
        char *request = NULL;
        Rio_readinitb(&rio, fd);
        for (;;) {
                Rio_readlineb(&rio, buf, MAXLINE);
                /*
                 * Firefox will sent \x16\x3.
                 * \x16: Synchronous Idle, \x3: End of Text
                 */
                if (!strncmp(buf, "\x16\x3", 2)) {
                        free(request);
                        return;
                }
                int buflen = strlen(buf);
                char *old_request = request;
                int old_reqlen = old_request==NULL ? 0 : strlen(old_request);
                request = realloc(old_request, buflen + old_reqlen + 1);
                if (request == NULL) {
                        fprintf(stderr, "realloc: run out of memory\n");
                        free(old_request);
                        return;
                }
                memmove(request+old_reqlen, buf, buflen+1);
                if (!strcmp(buf, "\r\n")) /* Copy before stop */
                        break;
        }
        sscanf(request, "%s %s %s", method, uri, version);
        if (strcasecmp(method, "GET")) {
                clienterror(fd, method, "501", "Not implemented",
                        "Tiny does not implement this method");
                return;
        }
        FILE *out = fopen("tiny-request.txt", "w+");
        if (out == NULL) {
                perror("fopen");
                free(request);
                return;
        }
        fprintf(out, "%s", request);
        fflush(out);
        fclose(out);
        free(request);

        /* Parse URI from GET request */
        is_static = parse_uri(uri, filename, cgiargs);
        if (stat(filename, &sbuf) < 0) {
                clienterror(fd, filename, "404", "Not found",
                            "Tiny couldn't read the file");
                return;
        }

        if (is_static) {        /* Serve static content */
                if (!(S_ISREG(sbuf.st_mode)) || !(S_IRUSR & sbuf.st_mode)) {
                        clienterror(fd, filename, "403", "Forbidden",
                                    "Tiny couldn't read the filetype");
                        return;
                }
                serve_static(fd, filename, sbuf.st_size);
        }
        else {                  /* Serve dynamic content */
                if (!(S_ISREG(sbuf.st_mode)) || !(S_IXUSR & sbuf.st_mode)) {
                        clienterror(fd, filename, "403", "Forbidden",
                                    "Tiny couldn't run the CGI program");
                        return;
                }
                serve_dynamic(fd, filename, cgiargs);
        }
}
Ejemplo n.º 13
0
void doit (int fd)  {
    int is_static;
    struct stat sbuf;
    char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE];
    char filename[MAXLINE], cgiargs[MAXLINE];
    rio_t rio;

    Rio_readinitb(&rio, fd);
    Rio_readlineb(&rio, buf, MAXLINE);

    sscanf(buf, "%s %s %s", method, uri, version);
    // get request 
    // e.g. GET / HTTP/1.1 

    if (!strcasecmp(method, "GET") || !strcasecmp(method, "HEAD")) {

        read_requesthdrs(&rio);

        int head = strcasecmp(method, "HEAD") ? 0 : 1;

        is_static = parse_uri(uri, filename, cgiargs);
        if (stat(filename, &sbuf) < 0) {
            clienterror(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(fd, filename, "403", "Forbidden", 
                        "Tiny couldn't read the file");
                return;
            }
            serve_static(fd, filename, sbuf.st_size, head);
        }

        else  { // serve dynamic content
            if(!(S_ISREG(sbuf.st_mode)) || !(S_IXUSR & sbuf.st_mode)) {
                clienterror(fd, filename, "403", "Forbidden", 
                        "Tiny couldn't run the CGI program");
                return;
            }
            serve_dynamic(fd, filename, cgiargs, head);
        }

    }

    else if (!strcmp(method, "POST")) {
        read_requesthdrs(&rio);
        parse_uri(uri, filename, cgiargs);
        Rio_readlineb(&rio, cgiargs, MAXLINE);
        Rio_readlineb(&rio, buf, MAXLINE);

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

        if(!(S_ISREG(sbuf.st_mode)) || !(S_IXUSR & sbuf.st_mode)) {
            clienterror(fd, filename, "403", "Forbidden", 
                    "Tiny couldn't run the CGI program");
            return;
        }
        serve_dynamic(fd, filename, cgiargs, 0);
    }

    else {
        clienterror(fd, method, "501", "Not Implemented", 
                "Tiny does not implement this method");
        return;
    }
}
Ejemplo n.º 14
0
// 处理HTTP事务
void* doit(void *arg)
{
    int fd = *((int *)arg);
    int is_static;
    struct stat sbuf;
    char buf[LINE_MAX] = {0}, method[LINE_MAX] = {0}, uri[LINE_MAX] = {0}, version[LINE_MAX] = {0};
    char filename[LINE_MAX] = {0}, cgiargs[LINE_MAX] = {0};

    do
    {
        // 读入请求行
        if (readline(fd, buf, sizeof(buf)) > 0)
        {
            if (sscanf(buf, "%s %s %s", method, uri, version) != 3)
            {
                client_error(fd, "GET", "501", "Not Impelemented", "error request line");
                break;
            }
        }

        // 读入请求报头,简单打印到标准输出
        while (readline(fd, buf, sizeof(buf)) > 0)
        {
            if (strcmp(buf, "\r\n") == 0)
                break;
        }

        /* 过滤所有非GET请求*/
        if (strcmp(method, "GET"))
        {
            client_error(fd, method, "501", "Not Impelementd", "minihttp does not impelement this method");
            break;
        }

        is_static = parse_uri(uri, filename, cgiargs);
        if (stat(filename, &sbuf) < 0)
        {
            client_error(fd, filename, "404", "Not found", "miniftp couldn't find this file");
            break;
        }

        if (is_static) /* Serve static content */
        {
            if (!(S_ISREG(sbuf.st_mode)) || !(S_IRUSR & sbuf.st_mode))
            {
                client_error(fd, filename, "403", "Forbidden", "minihttp couldn't read the file");
                break;
            }
            serve_static(fd, filename, sbuf.st_size);
        }
        else /* Serve dynamic content */
        {
            if (!(S_ISREG(sbuf.st_mode)) || !(S_IRUSR & sbuf.st_mode))
            {
                client_error(fd, filename, "403", "Forbidden", "minihttp couldn't run the CGI program");
                break;
            } 
            serve_dynamic(fd, filename, cgiargs);
        }
    } while (0);

    close(fd);
    free((int *)arg);
    return NULL;
}
Ejemplo n.º 15
0
// 
// name: 未知
// @param
// @return
//
void doit(int fd)
{
	printf("-------------------doit-----------------\n");
    int is_static;
    struct stat sbuf;
    int n;
    char buf[MAXLINE] = {0};
    char method[MAXLINE] = {0};
    char uri[MAXLINE] = {0};
    char version[MAXLINE] = {0};
    char filename[MAXLINE] = {0};
    char cgiargs[MAXLINE] = {0};
    rio_t rio;
    
    rio_readinitb(&rio, fd);
    n = rio_readlineb(&rio, buf, MAXLINE);
    if(n > 0)
    {
	    printf("Read [%d] bytes data from cache\n",n);	
	}
	else
	    printf("No data or Error\n");
    sscanf(buf, "%s %s %s", method, uri, version);
    printf("Method is [%s]\nUri is [%s]\nVersion is [%s]",method,uri,version);
    if(strcasecmp(method, "GET") != 0)
    {
	    clienterror(fd, method, "501", "Not Implemented",
	                 "Tiny does not implement this method");
	    return;	
	}
	/* read msg, and print*/
	read_requesthdrs(&rio);
	
	is_static = parse_uri(uri, filename, cgiargs);
	if(stat(filename, &sbuf) < 0)
	{
		printf("Can't find %s\n",filename);
	    clienterror(fd, filename, "404", "Not found",
	                 "Tiny couldn't find this file");
	    return;	
	}
	
	if(is_static)
	{
		if(!(S_ISREG(sbuf.st_mode))||!(S_IRUSR & sbuf.st_mode))
		{
		    clienterror(fd, filename, "403", "Forbidden",
	                     "Tiny couldn't read the file");
	        return;	
		}
		serve_static(fd, filename, sbuf.st_size);
	}
	else
	{
		if(!(S_ISREG(sbuf.st_mode))||!(S_IXUSR & sbuf.st_mode))
		{
		    clienterror(fd, filename, "403", "Forbidden",
	                     "Tiny couldn't run the CGI program");
	        return;
		}
		serve_dynamic(fd, filename, cgiargs);
	}
}
Ejemplo n.º 16
0
/* $begin doit */
void *doit(void *_fd) 
{
	int fd = (int)_fd;
    int is_static;
    struct stat sbuf;
    char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE];
    char filename[MAXLINE], cgiargs[MAXLINE];
    rio_t rio;
    fprintf(stderr," %d\n",fd);
 
    /* Read request line and headers */
    Rio_readinitb(&rio, fd);
    Rio_readlineb(&rio, buf, MAXLINE);                   //line:netp:doit:readrequest
    sscanf(buf, "%s %s %s", method, uri, version);       //line:netp:doit:parserequest
    if (strcasecmp(method, "GET") && strcasecmp(method,"POST")) {     //line:netp:doit:beginrequesterr
       clienterror(fd, method, "501", "Not Implemented",
                "Tiny does not implement this method");
       goto ext;
    }                                                    //line:netp:doit:endrequesterr
    read_requesthdrs(&rio);                              //line:netp:doit:readrequesthdrs

    /* Parse URI from GET request */
	fprintf(stderr,"URI: %s \n",uri);
	fprintf(stderr,"FILENAME: %s \n",filename);
    is_static = parse_uri(uri, filename, cgiargs);       //line:netp:doit:staticcheck
    if(!strcasecmp(method,"POST")){
    	Rio_readlineb(&rio, cgiargs, MAXLINE);
    	cgiargs[strlen(cgiargs)-2]=0;
	}
	fprintf(stderr,"ARG: %s \n",cgiargs);
	fprintf(stderr,"URI: %s \n",uri);
	fprintf(stderr,"FILENAME: %s \n",filename);
    if (is_static) { /* Serve static content */         
    	if (stat(filename, &sbuf) < 0 ) {                     //line:netp:doit:beginnotfound
			clienterror(fd, filename, "404", "Not found",
		    	"Tiny couldn't find this file");
			goto ext;
    	}                                                    //line:netp:doit:endnotfound

		if (!(S_ISREG(sbuf.st_mode)) || !(S_IRUSR & sbuf.st_mode)) { //line:netp:doit:readable
	    	clienterror(fd, filename, "403", "Forbidden",
				"Tiny couldn't read the file");
			goto ext;
		}
		serve_static(fd, filename, sbuf.st_size);        //line:netp:doit:servestatic
    }
    else { /* Serve dynamic content */
    	if (stat(filename, &sbuf) < 0 ) {                     //line:netp:doit:beginnotfound
   
			clienterror(fd, filename, "404", "Not found",
			    "Tiny couldn't find this file");
			goto ext;
 	    }                                                 //line:netp:doit:endnotfound
		if (!(S_ISREG(sbuf.st_mode)) || !(S_IXUSR & sbuf.st_mode)) { //line:netp:doit:executable
	    	clienterror(fd, filename, "403", "Forbidden",
				"Tiny couldn't run the CGI program");
			goto ext;
		}
		serve_dynamic(fd, filename, cgiargs);            //line:netp:doit:servedynamic
    }
    ext:
    Close(fd);
    return 0;
}