html - HTTP 获取 : send links of children and parent of the requested directory

标签 html c http directory

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

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

注意 1:要列出目录的内容,我使用 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>");

最佳答案

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

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

    改为这样写:

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

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

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

  • 除非需要,否则尽量避免动态内存分配,并且您在那里不需要它(加上检查 malloc 结果是否不为 null,如果 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 )
    
  • 最好:将指向父目录的链接放在页面顶部(即在打印循环之前)

关于html - HTTP 获取 : send links of children and parent of the requested directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35740507/

相关文章:

c - 使用 #define(添加结束大括号)进一步缩短 printf,或缩短 #define C

http - 使用 GET 进行非幂等请求

javascript - AngularJS 输入字段未从 Controller 内的 setTimeout 更新

php - 每次访问页面后提交表单都会发送空白信息

c++ - wchar_t 到 unsigned char 的转换

C:从未排序的链表中删除重复项

iphone - 使用 Cocoa/iPhoneSDK 获取 URL 的字符串内容的最快方法是什么?

javascript - 如何验证 HTTP 请求 header 中的 Do Not Track (DNT)?

html - 找出带有标题的三列布局的 CSS [包括草图]

javascript - 如何在单击按钮时加载下一个元素