【问题标题】:HTTP GET: send links of children and parent of the requested directoryHTTP GET:发送所请求目录的子目录和父目录的链接
【发布时间】:2016-06-14 21:49:54
【问题描述】:

我正在处理 HTTP GET 请求,我正在尝试做的事情如下:

如果请求对应一个目录并且该目录不包含index.html 文件,响应一个 HTML 页面,该页面包含指向所有直接子级的链接 目录(类似于 ls -1),以及到父目录的链接。 (一个链接到 父目录看起来像父目录)

note1:为了列出目录的内容,我使用 opendir() 和 readdir()。笔记2: – HTTP 中的链接可以使用相对路径或绝对路径。

它不工作。两个错误示例如下:

FAIL-1 获取根目录列表: 我向服务器发送了一个 HTTP 请求,然后我得到了一些 HTML。 我寻找到“/test_directory1”的链接,但找不到。 (注意:在 HTML 中包含相对路径的链接是相对于 '/' 解析的。) 但是,我确实找到了“/test_file.txt”的链接。 如何重现: 我创建了一个空目录,并在其中放置了一个 test_file.txt 文件和一个 test_directory1。 我使用 --files 选项启动了 ./httpserver。 我向 Web 服务器发送了一个 / 的 HTTP 请求,以便查看我创建的所有文件。

FAIL-2 获取子目录的目录列表: 我向服务器发送了一个 HTTP 请求,然后我得到了一些 HTML。 我寻找到“/test_directory1”的链接,但找不到。 (注意:在 HTML 中包含相对路径的链接是相对于 '/test_directory1/test_directory2/' 解析的。) 但是,我确实找到了“/test_directory1/test_directory2”的链接。 如何重现: 我创建了一个空目录并在其中放置了一个 test_directory1 目录。 然后我在 test_directory1 中放了一个 test_directory2。 然后我使用 --files 选项启动 ./httpserver。 我为 /test_directory1/test_directory2 发送了一个 HTTP 请求(路径末尾没有斜杠。 我在 test_directory2 中查找文件列表,其中应包含指向父目录 (/test_directory1/) 的链接。

/*if is directory but doesn't contain index.html, send links instead*/
  struct dirent *dp;
  http_start_response(fd, 200);
  http_send_header(fd, "Content-Type", "text/html");

  http_end_headers(fd);
  while( (dp = readdir(opendir(fullPath)))!= NULL){

    char * filename = malloc(256+1);
    strcpy(filename, dp -> d_name);
    char * linkString = malloc(256+256+18+1);
    strcpy(linkString, "<<a href=\"");
    strcat(linkString, filename);
    strcat(linkString, "\">");
    strcat(linkString, filename);
    strcat(linkString, "</a></");
    http_send_string(fd, linkString);
    free(filename);

    printf("Is directory but doesn't have index.html, so all links %s\n", linkString);
    free(linkString);

  }

  http_send_string(fd, "<html><a href=\"../\">Parent directory</a><html>");

【问题讨论】:

  • 为什么要在循环内执行 printf ?还有为什么在将标签直接打印到流中更安全时构建字符串,似乎打印输出的正确方法是http_send_string

标签: html c http directory


【解决方案1】:

我很抱歉这么说,但你的代码在很多层面上似乎都是错误的:

  • while 条件将在每个循环中进行评估,这意味着您的目录每次都会打开,直到它无法再打开为止。

    改为这样写:

    if ((dir = opendir (fullPath)) == NULL) {
        //error handling
    }
    while ((dp = readdir (dir)) != NULL) {
        // print tag here
    }
    
  • 您构建的标签错误,您的输出将是

    <<a href="filename">filename</a>>
    

    我真的不知道在这种情况下会显示什么不同的浏览器

  • 尽量避免动态内存分配,除非需要,而且你不需要它(加上检查 malloc 结果是否不为空,如果 malloc 因任何原因失败)

    // print each tag
    const char* const filename=dp -> d_name;
    http_send_string( fd, "<a href=\"" ); // start tag open 
    http_send_string( fd, filename ); // attribute value
    http_send_string( fd, "\">" ); // close start tag
    http_send_string( fd, filename ); // tag inner text 
    http_send_string( fd, "</a></br>" ); // closing tag ( with a line break )
    
  • 最好:将父目录的链接放在页面顶部(即在您的打印循环之前)

【讨论】:

  • 这是我见过的最好的诚实回答。谢谢!!这解决了我的问题,我的代码现在可以工作了。
猜你喜欢
  • 2013-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-30
  • 1970-01-01
  • 2014-05-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多