SocketStream and weird 0xA character

Lubos Pintes lubos.pintes at gmail.com
Sun Feb 17 08:53:23 PST 2013


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;
     }
   }



More information about the Digitalmars-d-learn mailing list