Beispiel #1
0
void	parse_command(t_client *client, char cmd[], int size)
{
  int	i;
  int	cmd_size;

  i = 0;
  while (i >= 0 && commands[i].cmd)
  {
    cmd_size = strlen(commands[i].cmd);
    if (strncmp(commands[i].cmd, cmd, cmd_size) == 0)
    {
      (commands[i].func)(client, &cmd[cmd_size], size - cmd_size);
      return ;
    }
    ++i;
  }
  printf("Commande no found :\n");
  write(1, cmd, size);
  printf("\n\n");
  if (i >= 0)
    send_not_found(client);
}
// ## on file open
static void on_file_open(uv_fs_t* req) {
    if(req != NULL) {
        int status = req->result;
        req_res_t* rr = (req_res_t*) req->data;
        if(rr != NULL) {
            rr->pending_requests--;
            
            // Check if an error occurred in an other handler.
            if (rr->dead) {
                req_res_free(rr);
            }
            else {
                char* path_buf;
                int r;
                DEBUG("on_file_open %s\n", rr->path);
                
                uv_fs_req_cleanup(req);
                free(req);
                
                // Check if `uv_fs_open` returned an error.
                if (status < 0) {
                    // Send an `404` response only on `File not found` and `Illegal on directory` error.
                    // Note: It is important to close the file on `EMFILE` and other errors.
                    if (status == UV_ENOENT || status == UV_EISDIR) {
                        send_not_found(rr);
                        //return;
                    }
                    else {
                    fprintf(stderr, "async open error '%s': %s\n", rr->path, uv_strerror(status));
                    // Close the file and free the memory.
                    req_res_free(rr);
                    //return;
                    }
                }
                else {
                    // TODO: Recheck if it is possible to fail in `on_file_open` and get `status < 0`.
                    uv_fs_t stat_req;
                    
                    r = uv_fs_stat(uv_default_loop(), &stat_req, rr->path, NULL);
                    if (r != 0) {
                        fprintf(stderr, "uv_fs_stat error '%s': %s\n", rr->path, uv_strerror(r));
                        req_res_free(rr);
                        //return;
                    }
                    else {
                        uv_stat_t* s = &stat_req.statbuf;
                        
                        if(s != NULL) {
                            if (!S_ISREG(s->st_mode)) {
                                // If the file is a directory append `index.html` to the path and reopen it.
                                if (S_ISDIR(s->st_mode)) {
                                    if (rr->path_len+10 >= MAX_PATH_SIZE) {
                                        fprintf(stderr, "headers buffer too small: -4");
                                        req_res_free(rr);
                                        //return;
                                    }
                                    else {
                                        /* ToDo: Please do not use magic numbers.*/
                                        path_buf = malloc(rr->path_len+11);
                                        if(path_buf != NULL) {
                                            
                                            memcpy(path_buf, rr->path, rr->path_len);
                                            memcpy(path_buf+rr->path_len, "index.html\0", 11);
                                            free(rr->path);
                                            rr->path = path_buf;
                                            rr->path_len += 10;
                                            // Set the content type.
                                            memcpy(rr->content_type, "text/html", 9);
                                            rr->content_type_len = 9;
                                            // Close the directory file handle.
                                            rr->file = status;
                                            req_res_free_file(rr);
                                            // Open the new path.
                                            open_path_as_file(rr);
                                        }
                                        else {
                                            DEBUG("on_file_open error: memory leak, no handle for path_buf with length %d.\n", rr->path_len+11);
                                        }
                                    }
                                } else {
                                    // Not a file or directory, send a `404`.
                                    send_not_found(rr);
                                }
                                //return;
                            }
                        
                            // Store the file handle.
                            rr->file = status;
                            // Send the `200` response headers.
                            rr->read_on_write = 1;
                            req_res_init_file_buf(rr);
                            // Send the `200` response headers.
                            set_headers(rr, "200 OK");
                            send_headers_buf(rr);
                        }
                        else {
                            DEBUG("on_file_open error: &stat_req.statbuf is NULL.\n");
                        }
                    }
                }
            }
        }
        else {
            DEBUG("on_file_open error: req->result is NULL.\n");
        }
    }
    else {
        DEBUG("on_file_open error: req is NULL.\n");
    }
}