我的 http 服务器中的数据未随 http 响应一起发送

发布于 2024-08-12 08:46:55 字数 1298 浏览 7 评论 0原文

我正在为我的班级制作一个 HTTP 服务器。但是,Firefox 不显示返回的文件。我构造一个响应标头,并将其放入要发送的文件的右侧 content-length 中,然后通过调用 send() 发送响应标头。接下来我再次使用send()发送文件,为什么Firefox不显示该文件?我将在下面发布相关代码。

正如您将在代码中看到的,我在 404 错误期间发送 error_response 时遇到问题。使用实时 http 标头,我可以看到 Firefox 收到了正确的响应标头,并且 cout <<错误响应<< endl; 确实打印出正确的响应。为什么 Firefox 不显示它?我是否只需要对包含标头和响应数据包的 send() 进行一次调用?

// send response headers
    response->Print( buffer, 2000 );
    if ( send( acc_tcp_sock, buffer, sizeof(buffer), 0 ) < 0 ) {
      cerr << "Unable to send response headers to client." << endl;
      return 5;
    }

    // if 200 and GET then send file
    if ( response->Get_code() == 200 && method == "GET" ) {
      // send file
    }

    // close file, if it has been opened
    if ( response->Get_code() == 200 ) {
      fclose( returned_file );
    }
    else if ( method == "GET" ) {
      if ( send( acc_tcp_sock, error_response.c_str(), error_response.length(), 0 ) < 0 ) {
        cerr << "Unable to send data to client." << endl;
        return 6;
      }
      cout << error_response << endl;
    }

    close( acc_tcp_sock ); // close the connection
  }

  return 0;
}

I'm working on making an HTTP server for my class. But, the returned file is not being displayed by Firefox. I construct a response header and put in the right content-length of the file I want to send, then I send the response headers with a call to send(). Next I use send() again to send the file, why is Firefox not displaying the file? I'll post the relevant code below.

As you'll see with the code, I am having trouble sending an error_response, during a 404 error. Using live http headers I can see that Firefox receives the correct response header, and the cout << error_response << endl; does print the correct response. Why doesn't Firefox display it? Do I need to make only one call to send() containing the header and the response packet?

// send response headers
    response->Print( buffer, 2000 );
    if ( send( acc_tcp_sock, buffer, sizeof(buffer), 0 ) < 0 ) {
      cerr << "Unable to send response headers to client." << endl;
      return 5;
    }

    // if 200 and GET then send file
    if ( response->Get_code() == 200 && method == "GET" ) {
      // send file
    }

    // close file, if it has been opened
    if ( response->Get_code() == 200 ) {
      fclose( returned_file );
    }
    else if ( method == "GET" ) {
      if ( send( acc_tcp_sock, error_response.c_str(), error_response.length(), 0 ) < 0 ) {
        cerr << "Unable to send data to client." << endl;
        return 6;
      }
      cout << error_response << endl;
    }

    close( acc_tcp_sock ); // close the connection
  }

  return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

予囚 2024-08-19 08:46:55

您是否尝试过在同一个发送命令中发送响应标头和文件?
有没有办法可以知道 Firefox 正在接收什么。您可以尝试使用一些 HTTPRequester 类编写一个控制台应用程序,该应用程序向您的服务器发送 HTTP 请求,并查看它得到什么回报。

Have you tried sending the response header and the file in the same send command?
Is there a way you can tell what firefox is receiving. You could try writing, using some HTTPRequester class, a console app that sends a HTTP request to your server and see what its getting in return.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文