Exemple #1
0
static void
do_file_response
(
    struct HTTPRequest *req,    /* HTTP request */
    FILE *out,                  /* output fd    */
    char *docroot               /* docroot path */
)
{
    struct FileInfo *info = NULL;

    dbg( "req=%p, out=%p, docroot=%p\n", req, out, docroot );

    info = get_fileinfo( docroot, req->path );
    if( 0 == info->ok )
    {
        free_fileinfo( info );
        not_found( req, out );
        return ;
    }

    output_common_header_fields( req, out, "200 OK" );

    fprintf( out, "Content-Length: %ld\r\n", info->size              );
    fprintf( out, "Content-Type: %s\r\n", guess_content_type( info ) );
    fprintf( out, "\r\n"                                             );

    outputBodyFields( info, req, out );

    fflush( out );
    free_fileinfo( info );
}
Exemple #2
0
static void
not_found(struct HTTPRequest *req, FILE *out)
{
    dbg( "req=%p, out=%p\n", req, out );

    output_common_header_fields( req, out, "404 Not Found" );

    fprintf( out, "Content-Type: text/html\r\n" );
    fprintf( out, "\r\n"                        );

    if( 0 != strcmp( req->method, "HEAD" ) )
    {
        fprintf( out, "<html>\r\n"                                   );
        fprintf( out, "<header><title>Not Found</title><header>\r\n" );
        fprintf( out, "<body><p>File not found</p></body>\r\n"       );
        fprintf( out, "</html>\r\n"                                  );
    }
    fflush(out);
}
Exemple #3
0
static void
not_implemented(struct HTTPRequest *req, FILE *out)
{
    dbg( "req=%p, out=%p\n", req, out );

    output_common_header_fields(req, out, "501 Not Implemented");

    fprintf( out, "Content-Type: text/html\r\n"                         );
    fprintf( out, "\r\n"                                                );
    fprintf( out, "<html>\r\n"                                          );
    fprintf( out, "<header>\r\n"                                        );
    fprintf( out, "<title>501 Not Implemented</title>\r\n"              );
    fprintf( out, "<header>\r\n"                                        );
    fprintf( out, "<body>\r\n"                                          );
    fprintf( out, "<p>The request method %s is not implemented</p>\r\n",
                    req->method                                         );
    fprintf( out, "</body>\r\n"                                         );
    fprintf( out, "</html>\r\n"                                         );

    fflush(out);
}
static void do_file_response(struct HTTPRequest *req, FILE *out, char *docroot) {
  struct FileInfo *info;

  info = get_fileinfo(docroot, req->path);
  if (!info->ok) {
    free_fileinfo(info);
    not_found(req, out);
    return;
  }
  output_common_header_fields(req, out, "200 OK");
  fprintf(out, "Content-Length: %ld\r\n", info->size);
  fprintf(out, "Content-Type: %s\r\n", guess_content_type(info));
  fprintf(out, "\r\n");
  if (strcmp(req->method, "HEAD") != 0) {
    int fd;
    char buf[BLOCK_BUF_SIZE];
    ssize_t n;

    fd = open(info->path, O_RDONLY);
    if (fd < 0) {
      log_exit("failed to open %s: %s", info->path, strerror(errno));
    }
    while (1) {
      n = read(fd, buf, BLOCK_BUF_SIZE);
      if (n < 0) {
          log_exit("failed to read %s: %s", info->path, strerror(errno));
      }
      if (n == 0) {
        break;
      }
      if (fwrite(buf, n, 1, out) < 1) {
        log_exit("failed to write to socket: %s", strerror(errno));
      }
    }
    close(fd);
  }
  fflush(out);
  free_fileinfo(info);
}
static void not_implemented(struct HTTPRequest *req, FILE *out) {
  output_common_header_fields(req, out, "501 Not Implemented");
}
static void method_not_allowd(struct HTTPRequest *req, FILE *out) {
  output_common_header_fields(req, out, "405 Method Not Allowed");
}
static void not_found(struct HTTPRequest *req, FILE *out) {
  output_common_header_fields(req, out, "404 Not Found");
}