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