Exemple #1
0
static int event_handler(sb_Event *e) {
  if (e->type == SB_EV_REQUEST) {
    printf("%s - %s %s\n", e->address, e->method, e->path);
    sb_send_status(e->stream, 200, "OK");
    sb_send_header(e->stream, "Content-Type", "text/plain");
    sb_writef(e->stream, "Hello world");
  }
  return SB_RES_OK;
}
Exemple #2
0
static int event_handler(sb_Event *e) {
  if (e->type == SB_EV_REQUEST) {
    char buf[512], *p;
    /* Log request */
    printf("%s - %s %s\n", e->address, e->method, e->path);
    /* Get var and replace non-alpha numeric / space chars with underscore */
    sb_get_var(e->stream, "q", buf, sizeof(buf));
    for (p = buf; *p; p++) {
      if ( !(isspace(*p) || isalnum(*p)) ) {
        *p = '_';
      }
    }
    /* Send page */
    sb_send_status(e->stream, 200, "OK");
    sb_send_header(e->stream, "Content-Type", "text/html");
    sb_writef(e->stream,
              "<!DOCTYPE html>"
              "<html><body>q = '%s'<br><br>"
              "<form method='post'><input type='text' name='q'></form>"
              "</body></html>", buf);
  }
  return SB_RES_OK;
}
Exemple #3
0
static int event_handler(sb_Event *e) {
  if (e->type == SB_EV_REQUEST) {
    const char *path;
    struct stat s;
    int err;

    /* Log request */
    printf("%s - %s %s\n", e->address, e->method, e->path);

    /* Is the requested path valid? */
    if (e->path[1] == '/' || strstr(e->path, ":/") || strstr(e->path, "..")) {
      sb_send_status(e->stream, 400, "Bad request");
      write_error_page(e->stream, "bad request");
      return SB_RES_OK;
    }

    /* Convert to filesystem path */
    if (e->path[1] == '\0') {
      path = ".";
    } else {
      path = e->path + 1;
    }

    /* Get file info */
    err = stat(path, &s);
    /* Does file / dir exist? */
    if (err == -1) {
      sb_send_status(e->stream, 404, "Not found");
      write_error_page(e->stream, "page not found");
      return SB_RES_OK;
    }

    /* Handle directory */
    if (S_ISDIR(s.st_mode)) {
      DIR *dir;
      struct dirent *ent;
      /* Send page header */
      sb_writef(e->stream, 
                "<html><head><title>Directory listing for %s</title></head>"
                "<body><h2>Directory listing for %s</h2><hr><ul>",
                e->path, e->path);
      /* Send file list */
      dir = opendir(path);
      while ((ent = readdir (dir)) != NULL) {
        if (!strcmp(ent->d_name, ".")) continue;
        if (!strcmp(ent->d_name, "..")) continue;
        sb_writef(e->stream, "<li><a href=\"/%s/%s\">%s%s</a></li>",
                  path, ent->d_name, ent->d_name,
                  ent->d_type == DT_DIR ? "/" : "");
      }
      closedir(dir);
      /* Send page footer */
      sb_writef(e->stream, "</ul></body></html>");
      return SB_RES_OK;
    }

    /* Handle file */
    err = sb_send_file(e->stream, path);
    if (err) {
      sb_send_status(e->stream, 500, "Internal server error"); 
      write_error_page(e->stream, sb_error_str(err));
    }
  }

  return SB_RES_OK;
}
Exemple #4
0
static void write_error_page(sb_Stream *st, const char *text) {
  sb_writef(st, "<html><body><h2>Error: %s</h2></body></html>\n", text);
}