Пример #1
0
/* Function to Build a response */
int buildresponse(http_request_t *http_request,http_status_t *http_status,http_response_t *http_response)
{
    int file_fd,length;
    ssize_t numread;
    char readbuf[MEMORYSIZE],writebuf[MEMORYSIZE];	
    http_header_t http_response_header;
    int headerindex=0;
    int position;
    char *uripath;

    uripath=strdup(http_request->uri);

    getdatetime(http_response);
    headerindex++;

    strncpy(http_response->resource_path,http_request->uri,PATH_MAX);

    char content_type[50];
    memset(content_type,0,sizeof(content_type));
    if(uripath)
        parsepath(uripath,content_type);
    strncpy(http_response->headers[POSITION_SERVER].field_name,"Server",MAX_HEADER_NAME_LENGTH);
    strncpy(http_response->headers[POSITION_SERVER].field_value,"CSUC",MAX_HEADER_VALUE_LENGTH);
    headerindex++;

    http_response->status.code=http_status->code;
    http_response->status.reason=http_status->reason;

    http_response->major_version=http_request->major_version;
    http_response->minor_version=http_request->minor_version;
	
    strncpy(http_response->headers[POSITION_CONTENTTYPE].field_name,"Content-Type",MAX_HEADER_NAME_LENGTH);
    headerindex++;

    strncpy(http_response->headers[POSITION_CONTENTLENGTH].field_name,"Content-Length",MAX_HEADER_NAME_LENGTH);	
    char lengthbuffer[MEMORYSIZE];
    int writecheck;
    if(http_status->code!=HTTP_STATUS_LOOKUP[POSITION_OK].code)
    {
        writecheck=checkforerrorfiles(http_status,http_response);
        strncpy(http_response->headers[POSITION_CONTENTTYPE].field_value,".html",MAX_HEADER_VALUE_LENGTH);
    }     
    else 
    {
        strncpy(http_response->headers[POSITION_CONTENTTYPE].field_value,content_type,MAX_HEADER_VALUE_LENGTH);
	strncpy(http_response->headers[POSITION_CONTENTLENGTH].field_name,"Content-Length",MAX_HEADER_NAME_LENGTH);
	length=find_content_length(http_request->uri,http_status);
	sprintf(lengthbuffer,"%d",length);
	strncpy(http_response->headers[POSITION_CONTENTLENGTH].field_value,lengthbuffer,MAX_HEADER_VALUE_LENGTH);
    }
    headerindex++;
    http_response->header_count=headerindex;

    free(uripath);
    return;
}
Пример #2
0
/*
** Process a single incoming HTTP request.
*/
static void win32_process_one_http_request(void *pAppData){
  HttpRequest *p = (HttpRequest*)pAppData;
  FILE *in = 0, *out = 0;
  int amt, got;
  int wanted = 0;
  char *z;
  char zRequestFName[MAX_PATH];
  char zReplyFName[MAX_PATH];
  char zCmd[2000];          /* Command-line to process the request */
  char zHdr[2000];          /* The HTTP request header */

  sqlite3_snprintf(MAX_PATH, zRequestFName,
                   "%s_in%d.txt", zTempPrefix, p->id);
  sqlite3_snprintf(MAX_PATH, zReplyFName,
                   "%s_out%d.txt", zTempPrefix, p->id);
  amt = 0;
  while( amt<sizeof(zHdr) ){
    got = recv(p->s, &zHdr[amt], sizeof(zHdr)-1-amt, 0);
    if( got==SOCKET_ERROR ) goto end_request;
    if( got==0 ){
      wanted = 0;
      break;
    }
    amt += got;
    zHdr[amt] = 0;
    z = strstr(zHdr, "\r\n\r\n");
    if( z ){
      wanted = find_content_length(zHdr) + (&z[4]-zHdr) - amt;
      break;
    }
  }
  if( amt>=sizeof(zHdr) ) goto end_request;
  out = fossil_fopen(zRequestFName, "wb");
  if( out==0 ) goto end_request;
  fwrite(zHdr, 1, amt, out);
  while( wanted>0 ){
    got = recv(p->s, zHdr, sizeof(zHdr), 0);
    if( got==SOCKET_ERROR ) goto end_request;
    if( got ){
      fwrite(zHdr, 1, got, out);
    }else{
      break;
    }
    wanted -= got;
  }
  fclose(out);
  out = 0;
  sqlite3_snprintf(sizeof(zCmd), zCmd, "\"%s\" http \"%s\" %s %s %s --nossl%s",
    g.nameOfExe, g.zRepositoryName, zRequestFName, zReplyFName,
    inet_ntoa(p->addr.sin_addr), p->zOptions
  );
  fossil_system(zCmd);
  in = fossil_fopen(zReplyFName, "rb");
  if( in ){
    while( (got = fread(zHdr, 1, sizeof(zHdr), in))>0 ){
      send(p->s, zHdr, got, 0);
    }
  }

end_request:
  if( out ) fclose(out);
  if( in ) fclose(in);
  closesocket(p->s);
  file_delete(zRequestFName);
  file_delete(zReplyFName);
  free(p);
}
Пример #3
0
int checkforerrorfiles(http_status_t *status,http_response_t *http_response)
{
    FILE *fpcheck;
    int file_fd,writecheck;
    //ssize_t numread;
    int numread;
    int checkfile=1,length;
    char *filename404=malloc(sizeof(char) * MEMORYSIZE);
    char *filename400=malloc(sizeof(char) * MEMORYSIZE);

    char readbuf[MEMORYSIZE],writebuf[MEMORYSIZE],lengthbuffer[MEMORYSIZE];
    strcpy(filename404,directory_name);
    strcat(filename404,"/404.html");

    strcpy(filename400,directory_name);
    strcat(filename400,"/400.html");
//    printf("%s\n",filename404);
//    printf("%s\n",filename400);
    switch(status->code)
    {
        case 404:
                fpcheck=fopen(filename404,"r");
                if(fpcheck==NULL)
                {
                    fpcheck=fopen(filename400,"r");
                    if(fpcheck==NULL)
                        checkfile=-1;    
                    else
                    {
                        length=find_content_length(filename400,status);
                        fclose(fpcheck);
                    }
                }
                else
                {
                    length=find_content_length(filename404,status);
                    fclose(fpcheck);
                }
                sprintf(lengthbuffer,"%d",length);
                strncpy(http_response->headers[POSITION_CONTENTLENGTH].field_value,lengthbuffer,MAX_HEADER_VALUE_LENGTH);
                break;
    
    }

    if(checkfile==-1)
    {
        switch(status->code)
        {
            case 404:
                file_fd=open(FILENOTFOUND,O_CREAT|O_RDWR,S_IRUSR|S_IWUSR);
                if(file_fd==-1)
                    logt(level,"File Open Error\n");    
                    //perror("File Open Error");
                else
                {
                    numread=read(file_fd,readbuf,MEMORYSIZE);
                    if(numread==0)
                    {
                        sprintf(writebuf,"404 Error-Page Not Found");
                        writecheck=write(file_fd,writebuf,strlen(writebuf));
                        if(writecheck==-1)
                            logt(level,"File Write Error\n");
                            //perror("File Write Error");
                        else
                        {
                            //sprintf(lengthbuffer,"%d",strlen(writebuf));
			    sprintf(lengthbuffer,"%d",24);
			    //strcpy(lengthbuffer,strlen(writebuf))
                        }
                    }
                    else
                        sprintf(lengthbuffer,"%d",numread);
                    close(file_fd);
                 }
        }
        strncpy(http_response->headers[POSITION_CONTENTLENGTH].field_value,lengthbuffer,MAX_HEADER_VALUE_LENGTH);
    }
    free(filename404);
    free(filename400);

    return checkfile;
}