]> git.ipfire.org Git - thirdparty/squid.git/blob - src/adaptation/ecap/MessageRep.cc
Merge fron trunk rev.13398
[thirdparty/squid.git] / src / adaptation / ecap / MessageRep.cc
1 /*
2 * DEBUG: section 93 eCAP Interface
3 */
4 #include "squid.h"
5 #include "BodyPipe.h"
6 #include "HttpReply.h"
7 #include "HttpRequest.h"
8 #include <libecap/common/names.h>
9 #include <libecap/common/area.h>
10 #include <libecap/common/version.h>
11 #include <libecap/common/named_values.h>
12 #include "adaptation/ecap/Host.h" /* for protocol constants */
13 #include "adaptation/ecap/MessageRep.h"
14 #include "adaptation/ecap/XactionRep.h"
15 #include "base/TextException.h"
16 #include "URL.h"
17
18 /* HeaderRep */
19
20 Adaptation::Ecap::HeaderRep::HeaderRep(HttpMsg &aMessage): theHeader(aMessage.header),
21 theMessage(aMessage)
22 {
23 }
24
25 bool
26 Adaptation::Ecap::HeaderRep::hasAny(const Name &name) const
27 {
28 const http_hdr_type squidId = TranslateHeaderId(name);
29 // XXX: optimize to remove getByName: we do not need the value here
30 return squidId == HDR_OTHER ?
31 theHeader.getByName(name.image().c_str()).size() > 0:
32 (bool)theHeader.has(squidId);
33 }
34
35 Adaptation::Ecap::HeaderRep::Value
36 Adaptation::Ecap::HeaderRep::value(const Name &name) const
37 {
38 const http_hdr_type squidId = TranslateHeaderId(name);
39 const String value = squidId == HDR_OTHER ?
40 theHeader.getByName(name.image().c_str()) :
41 theHeader.getStrOrList(squidId);
42 return value.size() > 0 ?
43 Value::FromTempString(value.termedBuf()) : Value();
44 }
45
46 void
47 Adaptation::Ecap::HeaderRep::add(const Name &name, const Value &value)
48 {
49 const http_hdr_type squidId = TranslateHeaderId(name); // HDR_OTHER OK
50 HttpHeaderEntry *e = new HttpHeaderEntry(squidId, name.image().c_str(),
51 value.toString().c_str());
52 theHeader.addEntry(e);
53
54 if (squidId == HDR_CONTENT_LENGTH)
55 theMessage.content_length = theHeader.getInt64(HDR_CONTENT_LENGTH);
56 }
57
58 void
59 Adaptation::Ecap::HeaderRep::removeAny(const Name &name)
60 {
61 const http_hdr_type squidId = TranslateHeaderId(name);
62 if (squidId == HDR_OTHER)
63 theHeader.delByName(name.image().c_str());
64 else
65 theHeader.delById(squidId);
66
67 if (squidId == HDR_CONTENT_LENGTH)
68 theMessage.content_length = theHeader.getInt64(HDR_CONTENT_LENGTH);
69 }
70
71 void
72 Adaptation::Ecap::HeaderRep::visitEach(libecap::NamedValueVisitor &visitor) const
73 {
74 HttpHeaderPos pos = HttpHeaderInitPos;
75 while (HttpHeaderEntry *e = theHeader.getEntry(&pos)) {
76 const Name name(e->name.termedBuf()); // optimize: find std Names
77 name.assignHostId(e->id);
78 visitor.visit(name, Value(e->value.rawBuf(), e->value.size()));
79 }
80 }
81
82 libecap::Area
83 Adaptation::Ecap::HeaderRep::image() const
84 {
85 MemBuf mb;
86 mb.init();
87
88 Packer p;
89 packerToMemInit(&p, &mb);
90 theMessage.packInto(&p, true);
91 packerClean(&p);
92 return Area::FromTempBuffer(mb.content(), mb.contentSize());
93 }
94
95 // throws on failures
96 void
97 Adaptation::Ecap::HeaderRep::parse(const Area &buf)
98 {
99 MemBuf mb;
100 mb.init();
101 mb.append(buf.start, buf.size);
102 Http::StatusCode error;
103 Must(theMessage.parse(&mb, true, &error));
104 }
105
106 http_hdr_type
107 Adaptation::Ecap::HeaderRep::TranslateHeaderId(const Name &name)
108 {
109 if (name.assignedHostId())
110 return static_cast<http_hdr_type>(name.hostId());
111 return HDR_OTHER;
112 }
113
114 /* FirstLineRep */
115
116 Adaptation::Ecap::FirstLineRep::FirstLineRep(HttpMsg &aMessage): theMessage(aMessage)
117 {
118 }
119
120 libecap::Version
121 Adaptation::Ecap::FirstLineRep::version() const
122 {
123 return libecap::Version(theMessage.http_ver.major,
124 theMessage.http_ver.minor);
125 }
126
127 void
128 Adaptation::Ecap::FirstLineRep::version(const libecap::Version &aVersion)
129 {
130 theMessage.http_ver.major = aVersion.majr;
131 theMessage.http_ver.minor = aVersion.minr;
132 }
133
134 libecap::Name
135 Adaptation::Ecap::FirstLineRep::protocol() const
136 {
137 // TODO: optimize?
138 switch (theMessage.http_ver.protocol) {
139 case AnyP::PROTO_HTTP:
140 return libecap::protocolHttp;
141 case AnyP::PROTO_HTTPS:
142 return libecap::protocolHttps;
143 case AnyP::PROTO_FTP:
144 return libecap::protocolFtp;
145 case AnyP::PROTO_GOPHER:
146 return libecap::protocolGopher;
147 case AnyP::PROTO_WAIS:
148 return libecap::protocolWais;
149 case AnyP::PROTO_WHOIS:
150 return libecap::protocolWhois;
151 case AnyP::PROTO_URN:
152 return libecap::protocolUrn;
153 case AnyP::PROTO_ICP:
154 return protocolIcp;
155 #if USE_HTCP
156 case AnyP::PROTO_HTCP:
157 return protocolHtcp;
158 #endif
159 case AnyP::PROTO_CACHE_OBJECT:
160 return protocolCacheObj;
161 case AnyP::PROTO_ICY:
162 return protocolIcy;
163 case AnyP::PROTO_COAP:
164 case AnyP::PROTO_COAPS: // use 'unknown' until libecap supports coap:// and coaps://
165 case AnyP::PROTO_UNKNOWN:
166 return protocolUnknown; // until we remember the protocol image
167 case AnyP::PROTO_NONE:
168 return Name();
169
170 case AnyP::PROTO_MAX:
171 break; // should not happen
172 // no default to catch AnyP::PROTO_ additions
173 }
174 Must(false); // not reached
175 return Name();
176 }
177
178 void
179 Adaptation::Ecap::FirstLineRep::protocol(const Name &p)
180 {
181 // TODO: what happens if we fail to translate some protocol?
182 theMessage.http_ver.protocol = TranslateProtocolId(p);
183 }
184
185 AnyP::ProtocolType
186 Adaptation::Ecap::FirstLineRep::TranslateProtocolId(const Name &name)
187 {
188 if (name.assignedHostId())
189 return static_cast<AnyP::ProtocolType>(name.hostId());
190 return AnyP::PROTO_UNKNOWN;
191 }
192
193 /* RequestHeaderRep */
194
195 Adaptation::Ecap::RequestLineRep::RequestLineRep(HttpRequest &aMessage):
196 FirstLineRep(aMessage), theMessage(aMessage)
197 {
198 }
199
200 void
201 Adaptation::Ecap::RequestLineRep::uri(const Area &aUri)
202 {
203 // TODO: if method is not set, urlPath will assume it is not connect;
204 // Can we change urlParse API to remove the method parameter?
205 // TODO: optimize: urlPath should take constant URL buffer
206 char *buf = xstrdup(aUri.toString().c_str());
207 const bool ok = urlParse(theMessage.method, buf, &theMessage);
208 xfree(buf);
209 Must(ok);
210 }
211
212 Adaptation::Ecap::RequestLineRep::Area
213 Adaptation::Ecap::RequestLineRep::uri() const
214 {
215 const char *fullUrl = urlCanonical(&theMessage);
216 Must(fullUrl);
217 // optimize: avoid copying by having an Area::Detail that locks theMessage
218 return Area::FromTempBuffer(fullUrl, strlen(fullUrl));
219 }
220
221 void
222 Adaptation::Ecap::RequestLineRep::method(const Name &aMethod)
223 {
224 if (aMethod.assignedHostId()) {
225 const int id = aMethod.hostId();
226 Must(Http::METHOD_NONE < id && id < Http::METHOD_ENUM_END);
227 Must(id != Http::METHOD_OTHER);
228 theMessage.method = HttpRequestMethod(static_cast<Http::MethodType>(id));
229 } else {
230 const std::string &image = aMethod.image();
231 theMessage.method = HttpRequestMethod(image.data(),
232 image.data() + image.size());
233 }
234 }
235
236 Adaptation::Ecap::RequestLineRep::Name
237 Adaptation::Ecap::RequestLineRep::method() const
238 {
239 switch (theMessage.method.id()) {
240 case Http::METHOD_GET:
241 return libecap::methodGet;
242 case Http::METHOD_POST:
243 return libecap::methodPost;
244 case Http::METHOD_PUT:
245 return libecap::methodPut;
246 case Http::METHOD_HEAD:
247 return libecap::methodHead;
248 case Http::METHOD_CONNECT:
249 return libecap::methodConnect;
250 case Http::METHOD_DELETE:
251 return libecap::methodDelete;
252 case Http::METHOD_TRACE:
253 return libecap::methodTrace;
254 default:
255 return Name(theMessage.method.image().toStdString());
256 }
257 }
258
259 libecap::Version
260 Adaptation::Ecap::RequestLineRep::version() const
261 {
262 return FirstLineRep::version();
263 }
264
265 void
266 Adaptation::Ecap::RequestLineRep::version(const libecap::Version &aVersion)
267 {
268 FirstLineRep::version(aVersion);
269 }
270
271 libecap::Name
272 Adaptation::Ecap::RequestLineRep::protocol() const
273 {
274 return FirstLineRep::protocol();
275 }
276
277 void
278 Adaptation::Ecap::RequestLineRep::protocol(const Name &p)
279 {
280 FirstLineRep::protocol(p);
281 }
282
283 /* ReplyHeaderRep */
284
285 Adaptation::Ecap::StatusLineRep::StatusLineRep(HttpReply &aMessage):
286 FirstLineRep(aMessage), theMessage(aMessage)
287 {
288 }
289
290 void
291 Adaptation::Ecap::StatusLineRep::statusCode(int code)
292 {
293 theMessage.sline.set(theMessage.sline.version, static_cast<Http::StatusCode>(code), theMessage.sline.reason());
294 }
295
296 int
297 Adaptation::Ecap::StatusLineRep::statusCode() const
298 {
299 // TODO: remove cast when possible
300 return static_cast<int>(theMessage.sline.status());
301 }
302
303 void
304 Adaptation::Ecap::StatusLineRep::reasonPhrase(const Area &str)
305 {
306 theMessage.sline.set(theMessage.sline.version, theMessage.sline.status(), str.toString().c_str());
307 }
308
309 Adaptation::Ecap::StatusLineRep::Area
310 Adaptation::Ecap::StatusLineRep::reasonPhrase() const
311 {
312 return Area::FromTempString(std::string(theMessage.sline.reason()));
313 }
314
315 libecap::Version
316 Adaptation::Ecap::StatusLineRep::version() const
317 {
318 return FirstLineRep::version();
319 }
320
321 void
322 Adaptation::Ecap::StatusLineRep::version(const libecap::Version &aVersion)
323 {
324 FirstLineRep::version(aVersion);
325 }
326
327 libecap::Name
328 Adaptation::Ecap::StatusLineRep::protocol() const
329 {
330 return FirstLineRep::protocol();
331 }
332
333 void
334 Adaptation::Ecap::StatusLineRep::protocol(const Name &p)
335 {
336 FirstLineRep::protocol(p);
337 }
338
339 /* BodyRep */
340
341 Adaptation::Ecap::BodyRep::BodyRep(const BodyPipe::Pointer &aBody): theBody(aBody)
342 {
343 }
344
345 void
346 Adaptation::Ecap::BodyRep::tie(const BodyPipe::Pointer &aBody)
347 {
348 Must(!theBody);
349 Must(aBody != NULL);
350 theBody = aBody;
351 }
352
353 Adaptation::Ecap::BodyRep::BodySize
354 Adaptation::Ecap::BodyRep::bodySize() const
355 {
356 return !theBody ? BodySize() : BodySize(theBody->bodySize());
357 }
358
359 /* MessageRep */
360
361 Adaptation::Ecap::MessageRep::MessageRep(HttpMsg *rawHeader):
362 theMessage(rawHeader), theFirstLineRep(NULL),
363 theHeaderRep(NULL), theBodyRep(NULL)
364 {
365 Must(theMessage.header); // we do not want to represent a missing message
366
367 if (HttpRequest *req = dynamic_cast<HttpRequest*>(theMessage.header))
368 theFirstLineRep = new RequestLineRep(*req);
369 else if (HttpReply *rep = dynamic_cast<HttpReply*>(theMessage.header))
370 theFirstLineRep = new StatusLineRep(*rep);
371 else
372 Must(false); // unknown message header type
373
374 theHeaderRep = new HeaderRep(*theMessage.header);
375
376 if (theMessage.body_pipe != NULL)
377 theBodyRep = new BodyRep(theMessage.body_pipe);
378 }
379
380 Adaptation::Ecap::MessageRep::~MessageRep()
381 {
382 delete theBodyRep;
383 delete theHeaderRep;
384 delete theFirstLineRep;
385 }
386
387 libecap::shared_ptr<libecap::Message>
388 Adaptation::Ecap::MessageRep::clone() const
389 {
390 HttpMsg *hdr = theMessage.header->clone();
391 hdr->body_pipe = NULL; // if any; TODO: remove pipe cloning from ::clone?
392 libecap::shared_ptr<libecap::Message> res(new MessageRep(hdr));
393
394 // restore indication of a body if needed, but not the pipe
395 if (theMessage.header->body_pipe != NULL)
396 res->addBody();
397
398 return res;
399 }
400
401 libecap::FirstLine &
402 Adaptation::Ecap::MessageRep::firstLine()
403 {
404 return *theFirstLineRep;
405 }
406
407 const libecap::FirstLine &
408 Adaptation::Ecap::MessageRep::firstLine() const
409 {
410 return *theFirstLineRep;
411 }
412
413 libecap::Header &
414 Adaptation::Ecap::MessageRep::header()
415 {
416 return *theHeaderRep;
417 }
418
419 const libecap::Header &
420 Adaptation::Ecap::MessageRep::header() const
421 {
422 return *theHeaderRep;
423 }
424
425 libecap::Body *
426 Adaptation::Ecap::MessageRep::body()
427 {
428 return theBodyRep;
429 }
430
431 void
432 Adaptation::Ecap::MessageRep::addBody()
433 {
434 Must(!theBodyRep);
435 Must(!theMessage.body_pipe); // set in tieBody()
436 theBodyRep = new BodyRep(NULL);
437 }
438
439 void
440 Adaptation::Ecap::MessageRep::tieBody(Adaptation::Ecap::XactionRep *x)
441 {
442 Must(theBodyRep != NULL); // addBody must be called first
443 Must(!theMessage.header->body_pipe);
444 Must(!theMessage.body_pipe);
445 theMessage.header->body_pipe = new BodyPipe(x);
446 theMessage.body_pipe = theMessage.header->body_pipe;
447 theBodyRep->tie(theMessage.body_pipe);
448 }
449
450 const libecap::Body *Adaptation::Ecap::MessageRep::body() const
451 {
452 return theBodyRep;
453 }