]> git.ipfire.org Git - thirdparty/squid.git/blob - src/adaptation/ecap/MessageRep.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / adaptation / ecap / MessageRep.cc
1 /*
2 * DEBUG: section 93 eCAP Interface
3 */
4 #include "squid.h"
5 #include "HttpRequest.h"
6 #include "HttpReply.h"
7 #include "BodyPipe.h"
8 #include "protos.h"
9 #include <libecap/common/names.h>
10 #include <libecap/common/area.h>
11 #include <libecap/common/version.h>
12 #include <libecap/common/named_values.h>
13 #include "adaptation/ecap/MessageRep.h"
14 #include "adaptation/ecap/XactionRep.h"
15 #include "adaptation/ecap/Host.h" /* for protocol constants */
16 #include "base/TextException.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.defined() ?
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_status 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.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_INTERNAL:
162 return protocolInternal;
163 case AnyP::PROTO_ICY:
164 return protocolIcy;
165 case AnyP::PROTO_COAP:
166 case AnyP::PROTO_COAPS: // use 'unknown' until libecap supports coap:// and coaps://
167 case AnyP::PROTO_UNKNOWN:
168 return protocolUnknown; // until we remember the protocol image
169 case AnyP::PROTO_NONE:
170 return Name();
171
172 case AnyP::PROTO_MAX:
173 break; // should not happen
174 // no default to catch AnyP::PROTO_ additions
175 }
176 Must(false); // not reached
177 return Name();
178 }
179
180 void
181 Adaptation::Ecap::FirstLineRep::protocol(const Name &p)
182 {
183 // TODO: what happens if we fail to translate some protocol?
184 theMessage.protocol = TranslateProtocolId(p);
185 }
186
187 AnyP::ProtocolType
188 Adaptation::Ecap::FirstLineRep::TranslateProtocolId(const Name &name)
189 {
190 if (name.assignedHostId())
191 return static_cast<AnyP::ProtocolType>(name.hostId());
192 return AnyP::PROTO_UNKNOWN;
193 }
194
195 /* RequestHeaderRep */
196
197 Adaptation::Ecap::RequestLineRep::RequestLineRep(HttpRequest &aMessage):
198 FirstLineRep(aMessage), theMessage(aMessage)
199 {
200 }
201
202 void
203 Adaptation::Ecap::RequestLineRep::uri(const Area &aUri)
204 {
205 // TODO: if method is not set, urlPath will assume it is not connect;
206 // Can we change urlParse API to remove the method parameter?
207 // TODO: optimize: urlPath should take constant URL buffer
208 char *buf = xstrdup(aUri.toString().c_str());
209 const bool ok = urlParse(theMessage.method, buf, &theMessage);
210 xfree(buf);
211 Must(ok);
212 }
213
214 Adaptation::Ecap::RequestLineRep::Area
215 Adaptation::Ecap::RequestLineRep::uri() const
216 {
217 const char *fullUrl = urlCanonical(&theMessage);
218 Must(fullUrl);
219 // optimize: avoid copying by having an Area::Detail that locks theMessage
220 return Area::FromTempBuffer(fullUrl, strlen(fullUrl));
221 }
222
223 void
224 Adaptation::Ecap::RequestLineRep::method(const Name &aMethod)
225 {
226 if (aMethod.assignedHostId()) {
227 const int id = aMethod.hostId();
228 Must(METHOD_NONE < id && id < METHOD_ENUM_END);
229 Must(id != METHOD_OTHER);
230 theMessage.method = HttpRequestMethod(static_cast<_method_t>(id));
231 } else {
232 const std::string &image = aMethod.image();
233 theMessage.method = HttpRequestMethod(image.data(),
234 image.data() + image.size());
235 }
236 }
237
238 Adaptation::Ecap::RequestLineRep::Name
239 Adaptation::Ecap::RequestLineRep::method() const
240 {
241 switch (theMessage.method.id()) {
242 case METHOD_GET:
243 return libecap::methodGet;
244 case METHOD_POST:
245 return libecap::methodPost;
246 case METHOD_PUT:
247 return libecap::methodPut;
248 case METHOD_HEAD:
249 return libecap::methodHead;
250 case METHOD_CONNECT:
251 return libecap::methodConnect;
252 case METHOD_DELETE:
253 return libecap::methodDelete;
254 case METHOD_TRACE:
255 return libecap::methodTrace;
256 default:
257 return Name(theMessage.method.image());
258 }
259 }
260
261 libecap::Version
262 Adaptation::Ecap::RequestLineRep::version() const
263 {
264 return FirstLineRep::version();
265 }
266
267 void
268 Adaptation::Ecap::RequestLineRep::version(const libecap::Version &aVersion)
269 {
270 FirstLineRep::version(aVersion);
271 }
272
273 libecap::Name
274 Adaptation::Ecap::RequestLineRep::protocol() const
275 {
276 return FirstLineRep::protocol();
277 }
278
279 void
280 Adaptation::Ecap::RequestLineRep::protocol(const Name &p)
281 {
282 FirstLineRep::protocol(p);
283 }
284
285 /* ReplyHeaderRep */
286
287 Adaptation::Ecap::StatusLineRep::StatusLineRep(HttpReply &aMessage):
288 FirstLineRep(aMessage), theMessage(aMessage)
289 {
290 }
291
292 void
293 Adaptation::Ecap::StatusLineRep::statusCode(int code)
294 {
295 // TODO: why is .status a enum? Do we not support unknown statuses?
296 theMessage.sline.status = static_cast<http_status>(code);
297 }
298
299 int
300 Adaptation::Ecap::StatusLineRep::statusCode() const
301 {
302 // TODO: see statusCode(code) TODO above
303 return static_cast<int>(theMessage.sline.status);
304 }
305
306 void
307 Adaptation::Ecap::StatusLineRep::reasonPhrase(const Area &)
308 {
309 // Squid does not support custom reason phrases
310 theMessage.sline.reason = NULL;
311 }
312
313 Adaptation::Ecap::StatusLineRep::Area
314 Adaptation::Ecap::StatusLineRep::reasonPhrase() const
315 {
316 return theMessage.sline.reason ?
317 Area::FromTempString(std::string(theMessage.sline.reason)) : Area();
318 }
319
320 libecap::Version
321 Adaptation::Ecap::StatusLineRep::version() const
322 {
323 return FirstLineRep::version();
324 }
325
326 void
327 Adaptation::Ecap::StatusLineRep::version(const libecap::Version &aVersion)
328 {
329 FirstLineRep::version(aVersion);
330 }
331
332 libecap::Name
333 Adaptation::Ecap::StatusLineRep::protocol() const
334 {
335 return FirstLineRep::protocol();
336 }
337
338 void
339 Adaptation::Ecap::StatusLineRep::protocol(const Name &p)
340 {
341 FirstLineRep::protocol(p);
342 }
343
344 /* BodyRep */
345
346 Adaptation::Ecap::BodyRep::BodyRep(const BodyPipe::Pointer &aBody): theBody(aBody)
347 {
348 }
349
350 void
351 Adaptation::Ecap::BodyRep::tie(const BodyPipe::Pointer &aBody)
352 {
353 Must(!theBody);
354 Must(aBody != NULL);
355 theBody = aBody;
356 }
357
358 Adaptation::Ecap::BodyRep::BodySize
359 Adaptation::Ecap::BodyRep::bodySize() const
360 {
361 return !theBody ? BodySize() : BodySize(theBody->bodySize());
362 }
363
364 /* MessageRep */
365
366 Adaptation::Ecap::MessageRep::MessageRep(HttpMsg *rawHeader):
367 theMessage(rawHeader), theFirstLineRep(NULL),
368 theHeaderRep(NULL), theBodyRep(NULL)
369 {
370 Must(theMessage.header); // we do not want to represent a missing message
371
372 if (HttpRequest *req = dynamic_cast<HttpRequest*>(theMessage.header))
373 theFirstLineRep = new RequestLineRep(*req);
374 else if (HttpReply *rep = dynamic_cast<HttpReply*>(theMessage.header))
375 theFirstLineRep = new StatusLineRep(*rep);
376 else
377 Must(false); // unknown message header type
378
379 theHeaderRep = new HeaderRep(*theMessage.header);
380
381 if (theMessage.body_pipe != NULL)
382 theBodyRep = new BodyRep(theMessage.body_pipe);
383 }
384
385 Adaptation::Ecap::MessageRep::~MessageRep()
386 {
387 delete theBodyRep;
388 delete theHeaderRep;
389 delete theFirstLineRep;
390 }
391
392 libecap::shared_ptr<libecap::Message>
393 Adaptation::Ecap::MessageRep::clone() const
394 {
395 HttpMsg *hdr = theMessage.header->clone();
396 hdr->body_pipe = NULL; // if any; TODO: remove pipe cloning from ::clone?
397 libecap::shared_ptr<libecap::Message> res(new MessageRep(hdr));
398
399 // restore indication of a body if needed, but not the pipe
400 if (theMessage.header->body_pipe != NULL)
401 res->addBody();
402
403 return res;
404 }
405
406 libecap::FirstLine &
407 Adaptation::Ecap::MessageRep::firstLine()
408 {
409 return *theFirstLineRep;
410 }
411
412 const libecap::FirstLine &
413 Adaptation::Ecap::MessageRep::firstLine() const
414 {
415 return *theFirstLineRep;
416 }
417
418 libecap::Header &
419 Adaptation::Ecap::MessageRep::header()
420 {
421 return *theHeaderRep;
422 }
423
424 const libecap::Header &
425 Adaptation::Ecap::MessageRep::header() const
426 {
427 return *theHeaderRep;
428 }
429
430 libecap::Body *
431 Adaptation::Ecap::MessageRep::body()
432 {
433 return theBodyRep;
434 }
435
436 void
437 Adaptation::Ecap::MessageRep::addBody()
438 {
439 Must(!theBodyRep);
440 Must(!theMessage.body_pipe); // set in tieBody()
441 theBodyRep = new BodyRep(NULL);
442 }
443
444 void
445 Adaptation::Ecap::MessageRep::tieBody(Adaptation::Ecap::XactionRep *x)
446 {
447 Must(theBodyRep != NULL); // addBody must be called first
448 Must(!theMessage.header->body_pipe);
449 Must(!theMessage.body_pipe);
450 theMessage.header->body_pipe = new BodyPipe(x);
451 theMessage.body_pipe = theMessage.header->body_pipe;
452 theBodyRep->tie(theMessage.body_pipe);
453 }
454
455 const libecap::Body *Adaptation::Ecap::MessageRep::body() const
456 {
457 return theBodyRep;
458 }