Esempio n. 1
0
void EP1_SERVER_respond (const EP1_NET_packet* req, EP1_NET_packet* resp) {
  /* Guarda a linha de requisição do pacote req */
  request_line reqline;
  /* Possível arquivo html mandado em resposta */
  response_file response;
  /* Possível informações do POST */
  post_info postinfo;
  /* Lê a linha de requisição do pacote req */
  parse_reqline(req->data, &reqline);
  /* Verifica se é método GET ou POST */
  if (strcmp(reqline.method, "GET") == 0) {
    /* Caso especial / redireciona para /index.html */
    if (strcmp(reqline.uri, "/") == 0)
      strcat(reqline.uri, "index.html");
    /* Tenta carregar página HTML */
    get_file(reqline.uri, &response);
    /* 200 OK */
    if (response.file != NULL)
      handle_ok(&response, resp);
    /* 404 NOT FOUND */
    else
      handle_notfound(reqline.uri, resp);
  } else if (strcmp(reqline.method, "POST") == 0) {
    /* Pega informações enviadas via POST*/
    get_postinfo(req->data, &postinfo);
    /* E monta a resposta */
    handle_post(&postinfo, resp);
  } else printf("[Método %s não suportado]\n", reqline.method);
}
Esempio n. 2
0
int handle(int newsockfd, struct sockaddr_in socket, socklen_t socklen)
{
    char buffer[256], path[PATH_MAX], *url;
    struct stat path_stat;
    int n;

    (void) socklen;

    bzero(buffer, 256);
    n = read(newsockfd, buffer, 255);
    if (n < 0)
        error("error reading");

    url = geturl(buffer);

    info("%s GET %s", inet_ntoa(socket.sin_addr), url);

    snprintf(path, PATH_MAX, "%s/%s", basedir, url);

    while (n == 255)
        n = read(newsockfd, buffer, 255);

    if (stat(path, &path_stat)) {
        handle_notfound(newsockfd);
    } else {
        if (S_ISDIR(path_stat.st_mode)) {
            if (path[strlen(path)-1] != '/') {
                size_t len;
                len = strlen(url);
                url = realloc(url, len + 2);
                url[len] = '/';
                url[len+1] = 0;
                handle_redirection(url, url, newsockfd);
            } else {
                struct stat index_stat;
                char index_path[PATH_MAX];
                snprintf(index_path, PATH_MAX, "%s/index.html", path);
                index_stat.st_mode = 0;
                stat(index_path, &index_stat);
                if (S_ISREG(index_stat.st_mode))
                    handle_file(url, index_path, newsockfd);
                else
                    handle_directory(url, path, newsockfd);
            }
        } else {
            handle_file(url, path, newsockfd);
        }
    }

    close(newsockfd);
    free(url);
    return 0;
}