OSDN Git Service

1.52.1.658
[gikonavigoeson/gikonavi.git] / ItemDownload.pas
index de6498e..c229d17 100644 (file)
@@ -53,7 +53,9 @@ type
                function ParseCgiStatus(Content: string): TCgiStatus;
                function CgiDownload(ItemType: TGikoDownloadType; URL: string; Modified: TDateTime): Boolean;
                function DatDownload(ItemType: TGikoDownloadType; URL: string; Modified: TDateTime; RangeStart: Integer; AdjustLen: Integer): Boolean;
-               function DeleteStatusLine(Content: string): string;
+               procedure DeleteStatusLine(Item: TDownloadItem);
+               procedure InitHttpClient(client: TIdHttp);
+               procedure ClearHttpClient(client: TIdHttp);
        protected
                procedure Execute; override;
        public
@@ -104,7 +106,7 @@ type
 implementation
 
 uses
-       Y_TextConverter;
+       Y_TextConverter, MojuUtils, HTMLCreate;
 
 constructor TDownloadThread.Create(CreateSuspended: Boolean);
 begin
@@ -118,13 +120,7 @@ end;
 
 destructor TDownloadThread.Destroy;
 begin
-    FIndy.Request.CustomHeaders.Clear;
-    FIndy.Request.RawHeaders.Clear;
-    FIndy.Request.Clear;
-    FIndy.Response.CustomHeaders.Clear;
-    FIndy.Response.RawHeaders.Clear;
-    FIndy.Response.Clear;
-    FIndy.ProxyParams.Clear;
+       ClearHttpClient(FIndy);
        FIndy.Free;
        inherited;
 end;
@@ -146,7 +142,65 @@ begin
                                                                                [Day, Copy(StrMonth, 1 + 3 * (Month - 1), 3),
                                                                                 Year, Hour, Min, Sec]);
 end;
-
+// ******************************************************************
+// HTTPClient\82Ì\8f\89\8aú\89»
+// ******************************************************************
+procedure TDownloadThread.InitHttpClient(client: TIdHttp);
+begin
+       ClearHttpClient(client);
+       client.Disconnect;
+       client.Request.UserAgent := GikoSys.GetUserAgent;
+       client.RecvBufferSize := Gikosys.Setting.RecvBufferSize;
+       client.ProxyParams.BasicAuthentication := False;
+       client.ReadTimeout := GikoSys.Setting.ReadTimeOut;
+       {$IFDEF DEBUG}
+       Writeln('------------------------------------------------------------');
+       {$ENDIF}
+       //FIndy.AllowCookies := False;
+       if GikoSys.Setting.ReadProxy then begin
+               if GikoSys.Setting.ProxyProtocol then
+                       client.ProtocolVersion := pv1_1
+               else
+                       client.ProtocolVersion := pv1_0;
+               client.ProxyParams.ProxyServer := GikoSys.Setting.ReadProxyAddress;
+               client.ProxyParams.ProxyPort := GikoSys.Setting.ReadProxyPort;
+               client.ProxyParams.ProxyUsername := GikoSys.Setting.ReadProxyUserID;
+               client.ProxyParams.ProxyPassword := GikoSys.Setting.ReadProxyPassword;
+               if GikoSys.Setting.ReadProxyUserID <> '' then
+                       client.ProxyParams.BasicAuthentication := True;
+               {$IFDEF DEBUG}
+               Writeln('\83v\83\8d\83L\83V\90Ý\92è\82 \82è');
+               Writeln('\83z\83X\83g: ' + GikoSys.Setting.ReadProxyAddress);
+               Writeln('\83|\81[\83g: ' + IntToStr(GikoSys.Setting.ReadProxyPort));
+               {$ENDIF}
+       end else begin
+               if GikoSys.Setting.Protocol then
+                       client.ProtocolVersion := pv1_1
+               else
+                       client.ProtocolVersion := pv1_0;
+               client.ProxyParams.ProxyServer := '';
+               client.ProxyParams.ProxyPort := 80;
+               client.ProxyParams.ProxyUsername := '';
+               client.ProxyParams.ProxyPassword := '';
+               {$IFDEF DEBUG}
+               Writeln('\83v\83\8d\83L\83V\90Ý\92è\82È\82µ');
+               {$ENDIF}
+       end;
+end;
+// ******************************************************************
+// HTTPClient\82Ì\83\8a\83N\83G\83X\83g\82Æ\83\8c\83X\83|\83\93\83X\82Ì\83f\81[\83^\82Ì\8fÁ\8b\8e
+// ******************************************************************
+procedure TDownloadThread.ClearHttpClient(client: TIdHttp);
+begin
+       client.Request.CustomHeaders.Clear;
+       client.Request.RawHeaders.Clear;
+       client.Request.Clear;
+       client.Response.CustomHeaders.Clear;
+       client.Response.RawHeaders.Clear;
+       client.Response.Clear;
+
+       client.ProxyParams.Clear;
+end;
 procedure TDownloadThread.Execute;
 var
        ResStream: TMemoryStream;
@@ -190,11 +244,11 @@ begin
                        begin
                                FDownloadTitle := FItem.FThreadItem.Title;
                                if FItem.FThreadItem <> nil then begin
-                    if FItem.FThreadItem.ParentBoard.IsBoardPlugInAvailable then begin
-                       boardPlugIn := FItem.FThreadItem.ParentBoard.BoardPlugIn;
-                        Item.State     := TGikoDownloadState( boardPlugIn.DownloadThread( DWORD( FItem.FThreadItem ) ) );
-                    end;
-                    //if FItem.FThreadItem.IsBoardPlugInAvailable then begin
+                                       if FItem.FThreadItem.ParentBoard.IsBoardPlugInAvailable then begin
+                                               boardPlugIn := FItem.FThreadItem.ParentBoard.BoardPlugIn;
+                                               Item.State      := TGikoDownloadState( boardPlugIn.DownloadThread( DWORD( FItem.FThreadItem ) ) );
+                                       end;
+                                       //if FItem.FThreadItem.IsBoardPlugInAvailable then begin
                                        //      boardPlugIn     := FItem.FThreadItem.BoardPlugIn;
                                        //      Item.State      := TGikoDownloadState( boardPlugIn.DownloadThread( DWORD( FItem.FThreadItem ) ) );
                                        //end;
@@ -217,51 +271,9 @@ begin
                        Continue;
                end;
 
-               //===== \83v\83\89\83O\83C\83\93\82ð\8eg\97p\82µ\82È\82¢\8fê\8d\87
                FAbort := False;
-               FIndy.Request.CustomHeaders.Clear;
-               FIndy.Response.Clear;
-               FIndy.Request.Clear;
-    FIndy.ProxyParams.Clear;
-    FIndy.Disconnect;
-               FIndy.Request.UserAgent := GikoSys.GetUserAgent;
-               FIndy.RecvBufferSize := Gikosys.Setting.RecvBufferSize;
-               FIndy.ProxyParams.BasicAuthentication := False;
-        FIndy.ReadTimeout := GikoSys.Setting.ReadTimeOut;
-               {$IFDEF DEBUG}
-               Writeln('------------------------------------------------------------');
-               {$ENDIF}
-               //FIndy.AllowCookies := False;
-               if GikoSys.Setting.ReadProxy then begin
-                       if GikoSys.Setting.ProxyProtocol then
-                               FIndy.ProtocolVersion := pv1_1
-                       else
-                               FIndy.ProtocolVersion := pv1_0;
-                       FIndy.ProxyParams.ProxyServer := GikoSys.Setting.ReadProxyAddress;
-                       FIndy.ProxyParams.ProxyPort := GikoSys.Setting.ReadProxyPort;
-                       FIndy.ProxyParams.ProxyUsername := GikoSys.Setting.ReadProxyUserID;
-                       FIndy.ProxyParams.ProxyPassword := GikoSys.Setting.ReadProxyPassword;
-                       if GikoSys.Setting.ReadProxyUserID <> '' then
-                               FIndy.ProxyParams.BasicAuthentication := True;
-                       {$IFDEF DEBUG}
-                       Writeln('\83v\83\8d\83L\83V\90Ý\92è\82 \82è');
-                       Writeln('\83z\83X\83g: ' + GikoSys.Setting.ReadProxyAddress);
-                       Writeln('\83|\81[\83g: ' + IntToStr(GikoSys.Setting.ReadProxyPort));
-                       {$ENDIF}
-               end else begin
-                       if GikoSys.Setting.Protocol then
-                               FIndy.ProtocolVersion := pv1_1
-                       else
-                               FIndy.ProtocolVersion := pv1_0;
-                       FIndy.ProxyParams.ProxyServer := '';
-                       FIndy.ProxyParams.ProxyPort := 80;
-                       FIndy.ProxyParams.ProxyUsername := '';
-                       FIndy.ProxyParams.ProxyPassword := '';
-                       {$IFDEF DEBUG}
-                       Writeln('\83v\83\8d\83L\83V\90Ý\92è\82È\82µ');
-                       {$ENDIF}
-               end;
-
+               //===== \83v\83\89\83O\83C\83\93\82ð\8eg\97p\82µ\82È\82¢\8fê\8d\87
+               InitHttpClient(FIndy);
                adjustMargin := 0;
                if Item.DownType = gdtThread then begin
                        if FileExists( Item.ThreadItem.GetThreadFileName ) then begin
@@ -434,6 +446,28 @@ begin
                                end;
 
                                //********************
+                               //dat.gz\81@\81¨\81@dat\82Ì\8eæ\93¾\81@2005\94N6\8c\8e\92Ç\89Á\81@by\82à\82\82ã
+                               //********************
+                               if (Item.DownType = gdtThread) and (Item.ResponseCode = 302) then begin
+                                       {$IFDEF DEBUG}
+                                       Writeln('dat\8eæ\93¾');
+                                       {$ENDIF}
+                                       FMsg := '\89ß\8b\8e\83\8d\83O(dat.gz)\82ª\91\8dÝ\82µ\82È\82¢\82½\82ß\89ß\8b\8e\83\8d\83O(dat)\82ð\92T\82µ\82Ü\82· - [' + ATitle + ']';
+                                       FIcon := gmiWhat;
+                                       if Assigned(OnDownloadMsg) then
+                                               Synchronize(FireDownloadMsg);
+                                       URL := ChangeFileExt(URL, '');
+                                       Modified := Item.ThreadItem.LastModified;
+                                       RangeStart := 0;
+                                       AdjustLen := 0;
+                                       if not DatDownload(Item.DownType, URL, Modified, RangeStart, AdjustLen) then
+                                               Item.State := gdsError;
+                                       {$IFDEF DEBUG}
+                                       Writeln('ResponseCode: ' + IntToStr(Item.ResponseCode));
+                                       {$ENDIF}
+                               end;
+
+                               //********************
                                //dat.gz\8eæ\93¾(2)
                                //********************
 {
@@ -498,8 +532,7 @@ begin
                                                                Writeln('CGIStatus: OK');
                                                                {$ENDIF}
                                                                Item.ResponseCode := 200;
-                                                               Item.Content := DeleteStatusLine(Item.Content);
-                                                               Item.ContentLength := CgiStatus.FSize;
+                                                               DeleteStatusLine(Item);
                                                        end;
                                                        gcsINCR: begin
                                                                //\8d¡\82Í\82 \82è\82¦\82È\82¢
@@ -507,8 +540,7 @@ begin
                                                                Writeln('CGIStatus: 206');
                                                                {$ENDIF}
                                                                Item.ResponseCode := 206;
-                                                               Item.Content := DeleteStatusLine(Item.Content);
-                                                               Item.ContentLength := CgiStatus.FSize;
+                                                               DeleteStatusLine(Item);
                                                        end;
                                                        gcsERR: begin
                                                                {$IFDEF DEBUG}
@@ -573,68 +605,9 @@ begin
                                        else
                                                Item.State := gdsError;
                                end;
-{
-                               //\96³\82¢\82Æ\8ev\82¤\82¯\82Ç\81B\81B\81B
-                               if (Item.ResponseCode in [200, 206]) and (Item.Content = '') then
-                                       Item.State := gdsError;
-                               Item.LastModified := FIndy.Response.LastModified;
-                               //\8d·\95ª\8eæ\93¾\82Å\82P\83o\83C\83g\91O\82©\82ç\82Æ\82Á\82Ä\82«\82½\82Æ\82«\82Í\83}\83C\83i\83X\82·\82é
-                               Item.ContentLength := FIndy.Response.ContentLength + AdjustLen;
-                               try
-                                       ResStream.Clear;
-                                       FIndy.Get(URL, ResStream);
-                                       Item.Content := GikoSys.GzipDecompress(ResStream, FIndy.Response.ContentEncoding);
-                                       if (Item.DownType = gdtThread) and (AdjustLen = -1) and (Item.Content[1] <> #10) then begin
-                                               //\8d·\95ª\8eæ\93¾\82©\82Â\82P\83o\83C\83g\96Ú\82ªLF\82Å\82È\82¢\8fê\8d\87\82Í\81u\82 \82Ú\81[\82ñ\81v\82³\82ê\82Ä\82¢\82é\82©\82à\82µ\82ê\82ñ\82Ì\82Å\8dÄ\8eæ\93¾
-                                               //\82±\82±\82Å\83\81\83b\83Z\81[\83W\95\\8e¦\83C\83x\83\93\83g
-                                               //event
-                                               FMsg := '\81u\82 \82Ú\81[\82ñ\81v\82ð\8c\9f\8fo\82µ\82½\82Ì\82Å\8dÄ\8eæ\93¾\82ð\8ds\82¢\82Ü\82·';
-                                               if Assigned(OnDownloadMsg) then
-                                                       Synchronize(FireDownloadMsg);
-                                               FIndy.Request.ContentRangeStart := 0;
-                                               FIndy.Request.ContentRangeEnd := 0;
-                                               AdjustLen := 0;
-                                               ResStream.Clear;
-                                               FIndy.Get(URL, ResStream);
-                                               Item.Content := GikoSys.GzipDecompress(ResStream, FIndy.Response.ContentEncoding);
-                                       end else if (Item.DownType = gdtThread) and (AdjustLen = -1) and (Item.Content[1] = #10) then begin
-                                               //\8d·\95ª\8eæ\93¾\82©\82Â\82P\83o\83C\83g\96Ú\82ªLF\82Ì\8fê\8d\87\81i\90³\8fí\8eæ\93¾\81j\82Í\93ª\82ÌLF\82ð\8dí\8f\9c
-                                               Item.Content := Copy(Item.Content, 2, Length(Item.Content));
-                                       end;
-                               except
-                                       Item.State := gdsError;
-                               end;
-                               Item.ResponseCode := FIndy.ResponseCode;
-}
-{
-                               try
-                                       ResStream.Clear;
-                                       FIndy.Get(URL, ResStream);
-                                       Item.Content := GikoSys.GzipDecompress(ResStream, FIndy.Response.ContentEncoding);
-                               except
-                                       Item.State := gdsError;
-                               end;
-
-                               CgiStatus := ParseCgiStatus(Item.Content);
-                               if CgiStatus.FStatus = gcsOK then begin
-                                       if CgiStatus.FSize = 0 then
-                                               Item.State := gdsNotModify
-                                       else if Item.DownType = gdtBoard then
-                                               Item.State := gdsComplete
-                                       else
-                                               Item.State := gdsDiffComplete;
-                               end else if CgiStatus.FStatus = gcsINCR then begin
-                                       Item.State := gdsComplete;
-                               end else if CgiStatus.FStatus = gcsERR then begin
-                                       Item.State := gdsError;
-                                       Item.ErrText := CgiStatus.FErrText;
-                               end;
-                               Item.ContentLength := CgiStatus.FSize;
-                               }
                        except
                                Item.State := gdsError;
                        end;
-                       //Item.ResponseCode := FIndy.ResponseCode;
                        if FAbort then
                                Item.State := gdsAbort;
                finally
@@ -643,13 +616,7 @@ begin
                        ResStream.Free;
                end;
 
-               FIndy.Request.CustomHeaders.Clear;
-        FIndy.Request.RawHeaders.Clear;
-        FIndy.Request.Clear;
-        FIndy.Response.CustomHeaders.Clear;
-        FIndy.Response.RawHeaders.Clear;
-               FIndy.Response.Clear;
-           FIndy.ProxyParams.Clear;
+               ClearHttpClient(FIndy);
 
                if Terminated then Break;
                Suspend;
@@ -741,8 +708,6 @@ var
 begin
        ResponseCode := -1;
        if (ItemType = gdtThread) and (RangeStart > 0) then begin
-//     if (ItemType = gdtThread) and (Item.ThreadItem.Size > 0) then begin
-//             FIndy.Request.ContentRangeStart := Item.ThreadItem.Size + AdjustLen;
                FIndy.Request.ContentRangeStart := RangeStart + AdjustLen;
                FIndy.Request.ContentRangeEnd := 0;
        end else begin
@@ -755,9 +720,7 @@ begin
        FIndy.Request.CustomHeaders.Add('Pragma: no-cache');
        if (Modified <> 0) and (Modified <> ZERO_DATE) then begin
                FIndy.Request.LastModified := modified - OffsetFromUTC;
-               //FIndy.Request.CustomHeaders.Add('If-Modified-Since: ' + RFC1123_Date(modified - OffsetFromUTC) + ' GMT');
        end;
-//     FIndy.Request.AcceptEncoding := 'gzip';
        if RangeStart = 0 then
                FIndy.Request.AcceptEncoding := 'gzip'
        else
@@ -771,11 +734,6 @@ begin
                        {$ENDIF}
                        FIndy.Get(URL, ResStream);
             Item.Content := GikoSys.GzipDecompress(ResStream, FIndy.Response.ContentEncoding);
-                       {$IFDEF DEBUG}
-                       if (FIndy.URL.Host = 'be.2ch.net') and (FIndy.URL.Path = '/be/') then begin
-                               Item.Content := EUCtoSJIS(Item.Content);
-                       end;
-            {$ENDIF}
                        Item.LastModified := FIndy.Response.LastModified;
                        //\8d·\95ª\8eæ\93¾\82Å\82P\83o\83C\83g\91O\82©\82ç\82Æ\82Á\82Ä\82«\82½\82Æ\82«\82Í\83}\83C\83i\83X\82·\82é
 //                     Item.ContentLength := FIndy.Response.ContentLength + AdjustLen;
@@ -931,17 +889,20 @@ begin
        end;
 end;
 
-//\8eè\94²\82«\82È\8f\88\97\9d\82Å1\8ds\96Ú\82ð\8fÁ\82·
-function TDownloadThread.DeleteStatusLine(Content: string): string;
+//\82P\8ds\96Ú\82ð\8fÁ\82µ\82Ä\81A\83R\83\93\83e\83\93\83c\83T\83C\83Y\82ð\90Ý\92è\82·\82é
+procedure TDownloadThread.DeleteStatusLine(Item: TDownloadItem);
 var
        SList: TStringList;
 begin
        SList := TStringList.Create;
        try
-               SList.Text := Content;
+               SList.Text := Item.Content;
+               //1\8ds\96Ú\82ð\8dí\8f\9c
                if SList.Count > 1 then
                        SList.Delete(0);
-               Result := SList.Text;
+        Item.Content := SList.Text;
+               //\89ü\8ds\83R\81[\83h\82ðCRLF -> LF\82Æ\8dl\82¦\82Ä\81A\8ds\90\94\95ª\82¾\82¯\83}\83C\83i\83X
+        Item.ContentLength := Length(SList.Text) - SList.Count;
        finally
                SList.Free;
        end;
@@ -955,7 +916,9 @@ var
        NumCount: Integer;
        Body: TStringList;
        Rec: TSubjectRec;
+       {$IFDEF DEBUG}
        st, rt : Cardinal;
+       {$ENDIF}
        function MakeThreadCallBack(
                inInstance      : DWORD;        // TBoardItem \82Ì\83C\83\93\83X\83^\83\93\83X
                inURL                           : PChar;        // \83X\83\8c\83b\83h\82Ì URL
@@ -1018,9 +981,9 @@ begin
 
                //dat\97\8e\82¿\83X\83\8c\82Ì\83\\81[\83g\8f\87\82ð\8c\88\92è\82·\82é\82½\82ß\82É\83\\81[\83g\82·\82é
                if GikoSys.Setting.DatOchiSortIndex >= 0 then begin
-                       Sort.SortNoFlag := true;
-                       Sort.SortOrder := GikoSys.Setting.DatOchiSortOrder;
-                       Sort.SortIndex := GikoSys.Setting.DatOchiSortIndex;
+                       Sort.SetSortNoFlag(true);
+                       Sort.SetSortOrder(GikoSys.Setting.DatOchiSortOrder);
+                       Sort.SetSortIndex(GikoSys.Setting.DatOchiSortIndex);
                        //Sort.SortNonAcquiredCountFlag := GikoSys.Setting.NonAcquiredCount;
                        Board.CustomSort(ThreadItemSortProc);
                end;
@@ -1066,7 +1029,7 @@ begin
                                        Board.Items[i].No                                               := Board.IntData;
                                        Board.Items[i].AllResCount      := Board.Items[i].Count;
                                        Board.Items[i].NewResCount      := 0;
-                                       Board.Items[i].AgeSage                  := gasNone;
+                                       Board.Items[i].AgeSage                  := gasArch;
                                end;
                        end;
 
@@ -1121,7 +1084,7 @@ begin
                                        Board.Items[i].No := NumCount;
                                        Board.Items[i].AllResCount := Board.Items[i].Count;
                                        Board.Items[i].NewResCount := 0;
-                                       Board.Items[i].AgeSage := gasNone;
+                                       Board.Items[i].AgeSage := gasArch;
                                end;
                        end;
                        //\83\8a\83X\83g(subject.txt)\82ð\95Û\91
@@ -1231,11 +1194,6 @@ var
        NewRes: Integer;
        finish : Boolean;
        loopCnt : Integer;
-//     KokoTxt : string;
-//     KokoIdx : Integer;
-//     NewTxt  : string;
-//     NewIdx  : Integer;
-//     LastTxt : string;
        LastIdx : Integer;
 begin
        FileName := ThreadItem.GetThreadFileName;
@@ -1334,7 +1292,7 @@ begin
                                Body.SaveToFile(FileName);
 
                                if ThreadItem.Title = '' then begin
-                                       Res := GikoSys.DivideStrLine(Body[0]);
+                                       THTMLCreate.DivideStrLine(Body[0], @Res);
                                        ThreadItem.Title := Res.FTitle;
                                end;
                                ThreadItem.Size := 0;
@@ -1356,7 +1314,6 @@ begin
        ThreadItem.RoundDate := Now;
        if not ThreadItem.UnRead then begin
                ThreadItem.UnRead := True;
-               ThreadItem.ParentBoard.UnRead := ThreadItem.ParentBoard.UnRead + 1;
        end;
 //     if ThreadItem.RoundNo = 6 then
 //             ThreadItem.RoundNo := 0;
@@ -1374,7 +1331,7 @@ begin
                ini.WriteInteger('Setting', 'NewResCount', ThreadItem.NewResCount);
                ini.WriteInteger('Setting', 'NewReceive', ThreadItem.NewReceive);
 //             ini.WriteInteger('Setting', 'RoundNo', ThreadItem.RoundNo);
-               ini.WriteBool('Setting', 'Round', ThreadItem.Round);
+//             ini.WriteBool('Setting', 'Round', ThreadItem.Round);
                ini.WriteBool('Setting', 'UnRead', ThreadItem.UnRead);
                ini.UpdateFile;
        finally