Главная страница
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.48 MB
Время: 0.03 c
6-1128509833
Profik
2005-10-05 14:57
2006.01.15
MailSlot


2-1135428146
DIS
2005-12-24 15:42
2006.01.15
Webbrowser Перехватить нажатие на элемементе submit


6-1127994898
Cameron
2005-09-29 15:54
2006.01.15
проблема передачи через сокеты не могу передать больше 8 кб


14-1134978880
Гриха
2005-12-19 10:54
2006.01.15
Миди


14-1135064032
stone
2005-12-20 10:33
2006.01.15
Это по-русски...