Exemplo n.º 1
0
void Serve_Resource(ReqInfo reqinfo, int conn) {
	int resource = 0;
	/*  Check whether resource exists, whether we have permission
     to access it, and update status code accordingly.          */
	if (reqinfo.status == 200)
		if ((resource = Check_Resource(&reqinfo)) < 0) {
			if (errno == EACCES)
				reqinfo.status = 401;
			else
				reqinfo.status = 404;
		}
	/*  Output HTTP response headers if we have a full request  */
	if (reqinfo.type == FULL)
		Output_HTTP_Headers(conn, &reqinfo);
	/*  Service the HTTP request  */
	//	if ( Return_Resource(conn, resource, &reqinfo) )
	//	    Error_Quit("Something wrong returning resource.");
	//    }
	//    else
	//	Return_Error_Msg(conn, &reqinfo);
    
	if (resource > 0)
		if (close(resource) < 0)
			Error_Quit("Error closing resource.");
	FreeReqInfo(&reqinfo);
}
Exemplo n.º 2
0
void *Service_Request(void *f) {

	int conn = (intptr_t)f;
    FILE *resource;
	struct ReqInfo  reqinfo;

    InitReqInfo(&reqinfo);
    
    /*  Get HTTP request  */
    if ( Get_Request(conn, &reqinfo) < 0 ) printf("error");
	//return -1;


	if (reqinfo.method == POST){
		handlePost(conn,&reqinfo);
	}
	if (strlen(reqinfo.resource) == 1) sprintf(reqinfo.resource,"/index.html");

/*  Check whether resource exists, whether we have permission
	to access it, and update status code accordingly.          */
	if ( reqinfo.status == 200 ){
		if ( (resource = Check_Resource(&reqinfo)) < 0 ) {
			if ( errno == EACCES )
			reqinfo.status = 401;
			else
			reqinfo.status = 404;
		}
	}
    /*  Output HTTP response headers if we have a full request  */

    if ( reqinfo.type == FULL )
	Output_HTTP_Headers(conn, &reqinfo);
    /*  Service the HTTP request  */
    if ( reqinfo.status == 200 ) {
		if ( Return_Resource(conn, resource, &reqinfo) ) syslog(LOG_NOTICE,"Something wrong returning resource.");
    }
    else Return_Error_Msg(conn, &reqinfo);

    if ( resource > 0 )
	if ( fclose(resource) < 0 ) syslog(LOG_NOTICE,"Error closing resource.");
    FreeReqInfo(&reqinfo);
	close(conn);
}