Example #1
0
File: genpage.c Project: jes/serve
/* Decides whether to send a dir listing or the index page and acts
   accordingly */
void send_dir(request *r) {
  char *file;
  int fildes;
  char *ptr;
  int i;

  /* try to get the index page */
  file = malloc(strlen(r->file) + /* strlen("/index.") */ 7 + longest_ext + 1);
  sprintf(file, "%s/index.%n", r->file, &i);
  ptr = file + i;/* ptr is the place where file extension should go */
  i = 0;

  /* go through each mimetype and try to open index */
  while(i < mimetypes) {
    strcpy(ptr, EXT(i++));
    fildes = open(file, O_RDONLY);
    if(fildes != -1) {/* index page can be opened for reading, send it */
      close(fildes);
      /* sort out our request structure */
      free(r->file);
      r->file = file;
      file_stuff(r);
      send_file(r);
      return;
    }
  }

  /* index page doesn't exist or can't be read, send dir list */
  free(file);
  send_dirlist(r);
}
Example #2
0
File: genpage.c Project: jes/serve
/* Generates and sends an error document */
void send_errorpage(request *r) {
  char *page = NULL;
  size_t len = 0;
  int fildes;
  char file[decimal_length(int) + 5 + 1];/* an int, .html, \0 */

  /* Look for a ready-made error document */
  snprintf(file, 9, "%d.html", r->status);
  if((fildes = open(file, O_RDONLY)) != -1) {/* file exists and is readable */
    close(fildes);
    /* set up the request structure */
    free(r->file);
    r->file = strdup(file);
    file_stuff(r);
    send_file(r);
    return;
  }

  /* Generate page */
  add_text(&page, &len, "<html><head><title>%d %s</title></head>\n", r->status,
           status_reason[r->status]);
  add_text(&page, &len, "<body><h1>%d %s</h1>\n", r->status,
           status_reason[r->status]);

  if(page_text[r->status]) 
    add_text(&page, &len, page_text[r->status]);
  else
    add_text(&page, &len, "See "
             "<a href=\"http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html\">"
             "this</a> for more information.");

  add_text(&page, &len, "\n</body></html>\n");

  /* And now send the page */
  free(r->content_type);
  r->content_type = strdup("text/html");

  r->encoding = IDENTITY;
  r->content_length = len;

  send_headers(r);
  send_str(r->fd, "\r\n");
  if(r->meth != HEAD) send(r->fd, page, len, 0);

  free(page);
}
Example #3
0
void main() {
  CS1_Window mw;
  int done;

  mw=cs1_make_window("white",100,100,(COLUMNS+2)*WIDTH,(ROWS+2)*HEIGHT);
  cs1_window_title(mw,"Peters Spreadsheet");  

  cs1_text_style(cs1_normal_style);
  cs1_set_rgb(0,0,0);
  Show_Board(mw);
  done=0;
  while (!done) {
    if (cs1_mouse_button(mw,cs1_left_button) && cs1_mouse_x(mw)<WIDTH/2 && cs1_mouse_y(mw)<HEIGHT) {
      cs1_set_color("black");
      cs1_draw_line(mw,0,0,0,HEIGHT);
      cs1_draw_line(mw,0,0,WIDTH/2,0);
      cs1_set_color("white");
      cs1_draw_line(mw,WIDTH/2,0,WIDTH/2,HEIGHT);
      cs1_draw_line(mw,0,HEIGHT,WIDTH/2,HEIGHT);

      file_stuff();
      Show_Board(mw);
    }
    if (cs1_mouse_button(mw,cs1_left_button) && cs1_mouse_x(mw)>WIDTH/2 && cs1_mouse_x(mw)<WIDTH && cs1_mouse_y(mw)<HEIGHT) {
      done=1;
    }
    if (cs1_mouse_x(mw)>=WIDTH||cs1_mouse_y(mw)>=HEIGHT) {
      if (cs1_mouse_button(mw,cs1_left_button)) {
        Input(cs1_mouse_x(mw)/WIDTH-1,cs1_mouse_y(mw)/HEIGHT-1);
        Show_Board(mw);
      }
      if (cs1_mouse_button(mw,cs1_right_button)) {
        Parse(cs1_mouse_x(mw)/WIDTH-1,cs1_mouse_y(mw)/HEIGHT-1);
        Show_Board(mw);
      }
    }
    if (cs1_inkey() == 'q') done=1;
  }
}
Example #4
0
File: request.c Project: jes/serve
/* creates a request structure with all the relevant information */
request *request_info(int fd, const char *addr, const char *req) {
  struct tm *tm_time;
  request *r;
  time_t tim;
  int len = PATH_MAX;

  r = calloc(1, sizeof(request));

  r->fd = fd;
  r->client = strdup((char*)addr);
  r->req = (char*)req;
  r->meth = method_type(req);
  r->reqfile = requ_file(req);
  r->file = filename(r->reqfile);
  r->http = http_version(req);
  r->status = 200;
  r->keep_alive = 300;

  /* get the document root */
  r->doc_root = malloc(len);
  while((getcwd(r->doc_root, len++)) == NULL)
    r->doc_root = realloc(r->doc_root, len);

  /* get date */
  time(&tim);
  tm_time = gmtime(&tim);
  r->date = malloc(64);
  strftime(r->date, 64, "%a, %d %b %Y %T GMT", tm_time);

  /* bad method */
  if(r->meth == INVALID) {
    r->status = 400;
    return r;
  }

  /* unsupported method? */
  if((r->meth != GET) &&
     (r->meth != HEAD) &&
     (r->meth != POST)) {
    r->status = 501;
    return r;
  }

  /* no http version */
  if(!r->http) {
    r->close_conn = 1;
    r->status = 400;
    return r;
  }

  /* unsupported major http version */
  if(strncmp(r->http, "HTTP/1.", 7) != 0)	{
    r->status = 505;
    return r;
  }

  /* check the file is proper */
  if(*r->reqfile != '/') {
    r->status = 400;
    return r;
  }

  /* close connection for HTTP 1.0 */
  if(strcmp(r->http, "HTTP/1.0") == 0) r->close_conn = 1;

  file_stuff(r);

  return r;
}