简体   繁体   English

简单的Http获得响应

[英]Simple Http Get Response

I have been writing a simple web server (http 1.0) for a class, but whenever I try to get a file ( wget 127.0.0.1 /filename ) is is short a few bytes. 我一直在为一个类编写一个简单的Web服务器(http 1.0),但每当我尝试获取一个文件( wget 127.0.0.1 /filename )时,只需几个字节。 The confusing thing is when I sum the number of sent bytes it matches the file size, but not the amount wget receives. 令人困惑的是,当我将发送的字节数与文件大小相匹配时,而不是wget接收的数量。

Why is wget not getting all of the data I write to the socket? 为什么wget没有得到我写入套接字的所有数据?

some wget output 一些wget输出

wget:

    --2012-10-27 19:02:00--  (try: 4)  http://127.0.0.1:5555/
    Connecting to 127.0.0.1:5555... connected.
    HTTP request sent, awaiting response... 200 Document follows
    Length: 5777 (5.6K) [text/html]
    Saving to: `index.html.6'

    99% [=====================================> ] 5,776       --.-K/s   in 0s      

    2012-10-27 19:02:00 (322 MB/s) - Read error at byte 5776/5777 (Connection reset by peer).  Retrying.


    --2012-10-27 19:03:52--  (try: 4)  http://127.0.0.1:5555/ZoEY8.jpg
    Connecting to 127.0.0.1:5555... connected.
    HTTP request sent, awaiting response... 200 Document follows
    Length: 163972 (160K) [image/jpeg]
    Saving to: `ZoEY8.jpg.4'

    91% [==================================>    ] 149,449     --.-K/s   in 0.001s  

    2012-10-27 19:03:52 (98.8 MB/s) - Read error at byte 163917/163972 (Connection reset by peer). Retrying.

Get method: 获取方法:

void *
processGetRequest(requestParser request)
{

  string resp= "HTTP/1.0 200 Document follows\r\nServer: lab5 \r\nContent-Length: ";
  string path="";
  path =request.path;

  //find file
  int page= open (path.c_str(),O_RDONLY);
  FILE * pageF= fdopen(page,"rb"); 


  //get size
  fseek(pageF, 0L, SEEK_END);
  int sz = ftell(pageF);
  fseek(pageF, 0L, SEEK_SET);

  //form content length
  stringstream ss;
  ss<<resp<<sz<<"\r\n";
  resp=ss.str(); 

  //make response
  if(page<0){
      cout<<"404 \n";
    resp = "HTTP/1.0 404 File Not Found\r\nServer: lab5 \r\nContent-type: text/html \r\n \r\n";

    write( request.fd, resp.c_str(), resp.length());

  return 0; 
  }
  if(path.find(".gif")!=string::npos)
   resp += "Content-type: image/gif\r\n \r\n";
  else if(path.find(".png")!=string::npos)
   resp += "Content-type: image/png\r\n \r\n";
  else if(path.find(".jpg")!=string::npos)
   resp += "Content-type: image/jpeg\r\n \r\n";
  else
   resp += "Content-type: text/html \r\n \r\n";

  //write response
  write( request.fd, resp.c_str(), resp.length());

  int total=0;      
  char buff[1024];
  int readBytes = 0;
  int er;

  //send file
  do{

  readBytes= read(page, buff, 1024);
  cout<<"read bytes "<<readBytes<<"\n";

  if(readBytes<0){
    perror("read");

    break;
  }
  total+=readBytes;
  er=  send( request.fd, buff, readBytes,0 );   
  cout<<"sent bytes "<<er<<"\n";
  if (er==-1){
    perror("send");
  }
  else if( er != readBytes){
    cout<<"Read write miss match\n";
  }

 }while(readBytes>0);

 close(page);
 return 0;
}

Edit: 编辑:

I have been working at this while and I'm wondering if Im doing my sockets wrong 我一直在这个工作,我想知道我是否做错了

// Set the IP address and port for this server
struct sockaddr_in serverIPAddress; 
memset( &serverIPAddress, 0, sizeof(serverIPAddress) );
serverIPAddress.sin_family = AF_INET;
serverIPAddress.sin_addr.s_addr = INADDR_ANY;
serverIPAddress.sin_port = htons((u_short) port);

// Allocate a socket
int masterSocket =  socket(PF_INET, SOCK_STREAM, 0);
if ( masterSocket < 0) {
  perror("socket");
  exit( -1 );
}

while ( 1 ) {

// Accept incoming connections
struct sockaddr_in clientIPAddress;
int alen = sizeof( clientIPAddress );
int slaveSocket = accept( masterSocket,
              (struct sockaddr *)&clientIPAddress,
              (socklen_t*)&alen);
// send slaveSocket to get method 
}

My first answer is below, but i just noticed something.. 我的第一个答案如下,但我只是注意到了一些事情......

"Content-type: text/html \r\n \r\n";

The headers must be separated from the content with two CR/LF. 必须使用两个CR / LF将标题与内容分开。 It looks like you have space in there 看起来你有空间

you can try this: 你可以试试这个:

 "Content-type: text/html\r\n\r\n";

Is the output buffer being correctly flushed and closed after the last write? 上次写入后输出缓冲区是否正确刷新并关闭? Try changing the size of your 1024 byte read buffer to something larger than your gif file. 尝试将1024字节读取缓冲区的大小更改为大于gif文件的大小。 This isnt a fix, but you may get different results, and this may help track down the cause of the problem. 这不是一个修复,但您可能会得到不同的结果,这可能有助于追踪问题的原因。 Maybe also put some logging into the read write loop. 也许还会将一些日志记录放入读写循环中。 See if the size of the last buffer write equals the number of bytes the response is missing. 查看最后一个缓冲区写入的大小是否等于响应丢失的字节数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM