1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire-method.cc,v 1.1 1998/10/30 07:53:35 jgg Exp $
4 /* ######################################################################
8 ##################################################################### */
10 // Include Files /*{{{*/
12 #pragma implementation "apt-pkg/acquire-method.h"
14 #include <apt-pkg/acquire-method.h>
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/configuration.h>
18 #include <apt-pkg/fileutl.h>
23 // AcqMethod::pkgAcqMethod - Constructor /*{{{*/
24 // ---------------------------------------------------------------------
25 /* This constructs the initialization text */
26 pkgAcqMethod::pkgAcqMethod(const char *Ver
,unsigned long Flags
)
30 strcat(End
,"100 Capabilities\n");
31 sprintf(End
+strlen(End
),"Version: %s\n",Ver
);
33 if ((Flags
& SingleInstance
) == SingleInstance
)
34 strcat(End
,"Single-Instance: true\n");
36 if ((Flags
& PreScan
) == PreScan
)
37 strcat(End
,"Pre-Scan: true\n");
39 if ((Flags
& Pipeline
) == Pipeline
)
40 strcat(End
,"Pipeline: true\n");
42 if ((Flags
& SendConfig
) == SendConfig
)
43 strcat(End
,"Send-Config: true\n");
46 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
50 // AcqMethod::Fail - A fetch has failed /*{{{*/
51 // ---------------------------------------------------------------------
53 void pkgAcqMethod::Fail()
55 string Err
= "Undetermined Error";
56 if (_error
->empty() == false)
57 _error
->PopMessage(Err
);
62 // AcqMethod::Fail - A fetch has failed /*{{{*/
63 // ---------------------------------------------------------------------
65 void pkgAcqMethod::Fail(string Err
)
68 snprintf(S
,sizeof(S
),"400 URI Failure\nURI: %s\n"
69 "Message %s\n\n",CurrentURI
.c_str(),Err
.c_str());
71 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
75 // AcqMethod::URIStart - Indicate a download is starting /*{{{*/
76 // ---------------------------------------------------------------------
78 void pkgAcqMethod::URIStart(FetchResult
&Res
,unsigned long Resume
= 0)
83 End
+= snprintf(S
,sizeof(S
),"200 URI Start\nURI: %s\n",CurrentURI
.c_str());
85 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Size: %u\n",Res
.Size
);
87 if (Res
.LastModified
!= 0)
88 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Last-Modified: %s\n",
89 TimeRFC1123(Res
.LastModified
).c_str());
92 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Resume-Point: %u\n",
96 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
100 // AcqMethod::URIDone - A URI is finished /*{{{*/
101 // ---------------------------------------------------------------------
103 void pkgAcqMethod::URIDone(FetchResult
&Res
, FetchResult
*Alt
)
108 End
+= snprintf(S
,sizeof(S
),"201 URI Done\nURI: %s\n",CurrentURI
.c_str());
110 if (Res
.Filename
.empty() == false)
111 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Filename: %s\n",Res
.Filename
.c_str());
114 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Size: %u\n",Res
.Size
);
116 if (Res
.LastModified
!= 0)
117 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Last-Modified: %s\n",
118 TimeRFC1123(Res
.LastModified
).c_str());
120 if (Res
.MD5Sum
.empty() == false)
121 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"MD5Sum: %s\n",Res
.MD5Sum
.c_str());
123 if (Res
.IMSHit
== true)
124 strcat(End
,"IMS-Hit: true\n");
129 if (Alt
->Filename
.empty() == false)
130 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-Filename: %s\n",Alt
->Filename
.c_str());
133 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-Size: %u\n",Alt
->Size
);
135 if (Alt
->LastModified
!= 0)
136 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-Last-Modified: %s\n",
137 TimeRFC1123(Alt
->LastModified
).c_str());
139 if (Alt
->MD5Sum
.empty() == false)
140 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-MD5Sum: %s\n",
141 Alt
->MD5Sum
.c_str());
143 if (Alt
->IMSHit
== true)
144 strcat(End
,"Alt-IMS-Hit: true\n");
148 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
152 // AcqMethod::Configuration - Handle the configuration message /*{{{*/
153 // ---------------------------------------------------------------------
154 /* This parses each configuration entry and puts it into the _config
155 Configuration class. */
156 bool pkgAcqMethod::Configuration(string Message
)
158 ::Configuration
&Cnf
= *_config
;
160 const char *I
= Message
.begin();
162 unsigned int Length
= strlen("Config-Item");
163 for (; I
+ Length
< Message
.end(); I
++)
166 if (I
[Length
] != ':' || stringcasecmp(I
,I
+Length
,"Config-Item") != 0)
171 for (; I
< Message
.end() && *I
== ' '; I
++);
172 const char *Equals
= I
;
173 for (; Equals
< Message
.end() && *Equals
!= '='; Equals
++);
174 const char *End
= Equals
;
175 for (; End
< Message
.end() && *End
!= '\n'; End
++);
179 Cnf
.Set(string(I
,Equals
-I
),string(Equals
+1,End
-Equals
-1));
186 // AcqMethod::Run - Run the message engine /*{{{*/
187 // ---------------------------------------------------------------------
189 int pkgAcqMethod::Run()
191 SetNonBlock(STDIN_FILENO
,true);
195 if (Messages
.empty() == true)
196 if (WaitFd(STDIN_FILENO
) == false)
199 if (ReadMessages(STDIN_FILENO
,Messages
) == false)
202 string Message
= Messages
.front();
203 Messages
.erase(Messages
.begin());
205 // Fetch the message number
207 int Number
= strtol(Message
.c_str(),&End
,10);
208 if (End
== Message
.c_str())
210 cerr
<< "Malformed message!" << endl
;
217 if (Configuration(Message
) == false)
223 CurrentURI
= LookupTag(Message
,"URI");
224 DestFile
= LookupTag(Message
,"FileName");
225 StrToTime(LookupTag(Message
,"Last-Modified"),LastModified
);
227 if (Fetch(Message
,CurrentURI
) == false)
237 // AcqMethod::FetchResult::FetchResult - Constructor /*{{{*/
238 // ---------------------------------------------------------------------
240 pkgAcqMethod::FetchResult::FetchResult() : LastModified(0),
241 IMSHit(false), Size(0)