Вниз
Скачать: 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.46 MB
Время: 0.036 c
2-1135424755
Новенькая
2005-12-24 14:45
2006.01.15
Динамический список


3-1132300203
Nortsov
2005-11-18 10:50
2006.01.15
передача файла через midas


2-1135509190
SergProger
2005-12-25 14:13
2006.01.15
Копирование файлов с сервера


10-1111149870
dewersia
2005-03-18 15:44
2006.01.15
помогите с com+


2-1135746291
mss
2005-12-28 08:04
2006.01.15
Проиграть два файла




   Наверх