Вниз
Скачать: CL | DM;

Сокет, как получить данные, если не известен размер данных   Найти похожие ветки 

← →
ILN ©   (2005-09-29 19:12) [0]

На основе примера "Learner Proxy", сделал проксю под себя, все нормально до тех пор, пока известен размер получаемых данных, как только сервер отправляет данные, которые формируется динамически, то все, пусто.. как быть?

 Server.CreateSocket;
 Server.Connect("127.0.0.1", "8080");

 FContentType := "";
 FContentLength := -1;
 Headers.Clear;
 repeat
   s := Client.RecvString(TimeOut);
   su := UpperCase(s);
   Headers.Add(s);
   Server.SendString(s + CRLF);
   if (Pos("CONTENT-LENGTH:", su) <> 0)
     then FContentLength := StrToIntDef(SeparateRight(su, " "), -1);
   if (Pos("CONTENT-TYPE:", su) <> 0)
     then FContentType := SeparateRight(s, " ");
 until (s = "") or (Client.LastError <> 0);
 
 WriteHeaders(0);

 if (FContentLength <> -1) then
 begin
   Stream.Clear;
   Stream.SetSize(FContentLength);
   Client.RecvBufferEx(Stream.Memory, FContentLength, TimeOut);
   Stream.Seek(0, soFromBeginning);
   Server.SendBuffer(Stream.Memory, FContentLength);
   Stream.Seek(0, soFromBeginning);
   WriteContent(0);
 end;
 
 
 // Read From Server
 
 Chunked := False;
 FContentType := "";
 FContentLength := -1;
 Headers.Clear;
 if Server.CanRead(TimeOut) then
 begin
   repeat
     s := Server.RecvString(TimeOut);
     Headers.Add(s);
     Client.SendString(s + CRLF);
     su := UpperCase(s);
     if (Pos("CONTENT-LENGTH:", su) <> 0)
       then FContentLength := StrToIntDef(SeparateRight(su, " "), -1);
     if (Pos("CONTENT-TYPE:", su) <> 0)
       then FContentType := SeparateRight(s, " ");
     if (su = "TRANSFER-ENCODING: CHUNKED")
       then Chunked := True;
   until (s = "") or (Server.LastError <> 0);
 
   WriteHeaders(1);
 
   if Chunked then
   begin
     repeat
       s := Server.RecvString(TimeOut);
       Client.SendString(s + CRLF);
       ToWriteLn := s;
       DoWriteLn(1);
       if (s = "0") then Break;
       FContentLength := StrToInt("$" + Trim(s)); // Trim. Trim. Trim. Why they send trailing spaces?
       Stream.Clear;
       Stream.SetSize(FContentLength);
       Server.RecvBufferEx(Stream.Memory, FContentLength, TimeOut);
       Stream.Seek(0, soFromBeginning);
       Client.SendBuffer(Stream.Memory, FContentLength);
       Stream.Seek(0, soFromBeginning);
       WriteContent(1);
 
       s := Server.RecvString(TimeOut);
       Client.SendString(s + CRLF);

     until False;
   end else
   begin
     if (FContentLength <> -1) then
     begin
       Stream.Clear;
       Stream.SetSize(FContentLength);
       Server.RecvBufferEx(Stream.Memory, FContentLength, TimeOut);
       Stream.Seek(0, soFromBeginning);
       Client.SendBuffer(Stream.Memory, FContentLength);
       Stream.Seek(0, soFromBeginning);
       WriteContent(1);
     end;
   end;

 end else
 begin
   Client.SendString("HTTP/1.1 200 OK" + CRLF);
   Client.SendString("Content-Type: text/html" + CRLF);
   Client.SendString("Transfer-Encoding: chunked" + CRLF);
   Client.SendString("Connection: close" + CRLF);
   Client.SendString(CRLF);
   s := "<html><body>Response Error from HTTP Server (Timed Out))</body></html>";
   Client.SendString(IntToHex(Length(s), 2) + CRLF);
   Client.SendString(s + CRLF);
 end;

 Synchronize(LogAll);

 Client.CloseSocket;
 Server.CloseSocket;

особенно это касается строчки if (FContentLength <> -1) then


← →
Piter ©   (2005-09-29 21:53) [1]

Вряд ли кто будет разбираться в этом кривом коде


← →
Piter ©   (2005-09-29 21:54) [2]

:)



Страницы: 1 вся ветка

Скачать: CL | DM;



Память: 0.45 MB
Время: 0.029 c
14-1134839841
Piter
2005-12-17 20:17
2006.01.15
Хазанов


14-1135319571
syte_ser78
2005-12-23 09:32
2006.01.15
еще один вопрос по хостингу :)


3-1132562149
diwww
2005-11-21 11:35
2006.01.15
Как обновлять базу после установки галочки?


2-1135275962
denisww
2005-12-22 21:26
2006.01.15
HTTPRIO


14-1134882689
Вопрос
2005-12-18 08:11
2006.01.15
А вас, господа, уважаемые, взрослые программисты,




   Наверх