www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - SocketStream and weird 0xA character

reply Lubos Pintes <lubos.pintes gmail.com> writes:
I am writing little program which downloads simple data file from server 
through HTTP.
The file is static, but updated regularly, so I am using "Range: bytes" 
header to optimize the traffic a bit.
After I analyzed HTTP status line and headers, I started to read the raw 
data through SocketStream.read(buffer).
There seems to be one single '\n' character in buffer[0] after first 
read. I cannot figure out why this character is appearing. It is not 
there when I download that file through wget.
Here is relevant part of code:
//Parse status line + headers
   string[string] header;
   auto line=ss.readLine();
   auto statusLine=line.split(" ");
   auto responseCode=to!int(statusLine[1]);
   while(true) {
     line=ss.readLine();
     if(!line.length) break;
     auto h=line.split(":");
     header[h[0].idup]=h[1].strip.idup;
   }
   int contentLength=to!uint(header["Content-Length"]);
   if(responseCode==416 && contentLength==fileSize) return; //nothing to 
download
   if(responseCode==200 || responseCode==216) {
     ubyte[] buffer=new ubyte[4096];
     auto first=true;
     while(contentLength>0) {
       auto bytesRead=ss.read(buffer);
       if(first) {writeln(buffer[0..20]); first=false; }
       f.rawWrite(buffer[0..bytesRead]);
       contentLength-=bytesRead;
     }
   }
Feb 17 2013
parent "Regan Heath" <regan netmail.co.nz> writes:
On Sun, 17 Feb 2013 16:53:23 -0000, Lubos Pintes <lubos.pintes gmail.com>  
wrote:

 I am writing little program which downloads simple data file from server  
 through HTTP.
 The file is static, but updated regularly, so I am using "Range: bytes"  
 header to optimize the traffic a bit.
 After I analyzed HTTP status line and headers, I started to read the raw  
 data through SocketStream.read(buffer).
 There seems to be one single '\n' character in buffer[0] after first  
 read. I cannot figure out why this character is appearing. It is not  
 there when I download that file through wget.
 Here is relevant part of code:
 //Parse status line + headers
    string[string] header;
    auto line=ss.readLine();
    auto statusLine=line.split(" ");
    auto responseCode=to!int(statusLine[1]);
    while(true) {
      line=ss.readLine();
      if(!line.length) break;
      auto h=line.split(":");
      header[h[0].idup]=h[1].strip.idup;
    }
    int contentLength=to!uint(header["Content-Length"]);
    if(responseCode==416 && contentLength==fileSize) return; //nothing to  
 download
    if(responseCode==200 || responseCode==216) {
      ubyte[] buffer=new ubyte[4096];
      auto first=true;
      while(contentLength>0) {
        auto bytesRead=ss.read(buffer);
        if(first) {writeln(buffer[0..20]); first=false; }
        f.rawWrite(buffer[0..bytesRead]);
        contentLength-=bytesRead;
      }
    }
IIRC there is a blank line after headers in the HTTP protocol, that's how you know you're at the end of the headers i.e. <status code/response> <header> [<header>] <blank line> <body> R -- Using Opera's revolutionary email client: http://www.opera.com/mail/
Mar 20 2013