Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2006.01.15;
Скачать: 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 вся ветка

Текущий архив: 2006.01.15;
Скачать: CL | DM;

Наверх




Память: 0.47 MB
Время: 0.07 c
3-1131965689
Id
2005-11-14 13:54
2006.01.15
Ошибка при создании таблицы


2-1135271634
oleggar
2005-12-22 20:13
2006.01.15
queryperformance


14-1134738594
cherman
2005-12-16 16:09
2006.01.15
Сколько стоит написать программу


10-1111350792
TheEd
2005-03-20 23:33
2006.01.15
Как в Excel нарисовать границу между двумя ячейками (областями)?


14-1134656616
IceBeerg
2005-12-15 17:23
2006.01.15
Параметры regedit а