示例#1
0
文件: requests.c 项目: 19fever/sws
/* return 0 if success */
int serve_request(int fd, Req_info *req){
    struct stat sbuf;
    if(stat(req->uri, &sbuf) < 0){
        req->status = 404;
        sws_response(fd, req);
        return 1;
    }
    
    if (check_modified_since(req, &sbuf)) {
        req->status = 304;
        sws_response(fd, req);
        return 1;
    }
    
    if(req->cgi == NO_CGI){        /* static */
        if( (S_ISREG(sbuf.st_mode)) && (S_IRUSR & sbuf.st_mode)) {
            return serve_static(fd, req, sbuf.st_size);
            
        }
        else if ((S_ISDIR(sbuf.st_mode)) && (S_IRUSR & sbuf.st_mode)) {            
            return serve_dir(fd, req);
        }
        else {
            req->status = 403;
            sws_response(fd, req);
            return 1;
        }        
    }
    else {    
        /* dynamic */
        if(!(S_ISREG(sbuf.st_mode)) || !(S_IXUSR & sbuf.st_mode)) {
            req->status = 403;
            sws_response(fd, req);
            return 1;
        }
        /* req' uri*/
        if (req->method == GET)
            return serve_GET_dynamic(fd, req);
        else if (req->method == POST) {
            if (req->contLen==0) {
                req->status=400;
                sws_response(fd, req);
                return 1;
            }
            return serve_POST_dynamic(fd, req);
        }
    }
    return 0;
}
示例#2
0
/* $begin doit */
void doit(int fd) 
{
    int is_static,contentLength=0,isGet=1;
    struct stat sbuf;
    char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE];
    char filename[MAXLINE], cgiargs[MAXLINE],httpspostdata[MAXLINE];
    rio_t rio;

    memset(buf,0,MAXLINE);	
    if(ishttps)
    {
        ssl=SSL_new(ssl_ctx);
	SSL_set_fd(ssl,fd);
	if(SSL_accept(ssl)==0)
	{
		ERR_print_errors_fp(stderr);
		exit(1);
	}
	SSL_read(ssl,buf,sizeof(buf));
	printf(".............\n");
	printf("%s",buf);
	printf(".............\n");
    }
    else
    {
	/* 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&&strcasecmp(method,"POST")!=0) 
    { 
       clienterror(fd, method, "501", "Not Implemented",
                "Tiny does not implement this method");
        return;
    }
    */

    
    /* Parse URI from GET request */
    is_static = parse_uri(uri, filename, cgiargs);
    

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

    if(S_ISDIR(sbuf.st_mode)&&isShowdir)
    	serve_dir(fd,filename);


    if (strcasecmp(method, "POST")==0) 
    	isGet=0;


    if (is_static) 
    { /* Serve static content */
    	if(!ishttps) 
    		get_requesthdrs(&rio);  /* because https already read the headers -> SSL_read()  */

	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;
	}

	if(isGet)
	{
		if(!ishttps)
			get_requesthdrs(&rio);   /* because https already read headers by SSL_read() */

		get_dynamic(fd, filename, cgiargs);
	}
	else
	{
		printf("ishttps:%d\n",ishttps);
		if(ishttps)
			https_getlength(buf,&contentLength);
		else
			post_requesthdrs(&rio,&contentLength);

		post_dynamic(fd, filename,contentLength,&rio);
	}
    }
}