]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/adaptation/ecap/MessageRep.cc
bade95794f5b6d043fa0dd157645e5bebca0a900
2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 93 eCAP Interface */
13 #include "HttpReply.h"
14 #include "HttpRequest.h"
15 #include <libecap/common/names.h>
16 #include <libecap/common/area.h>
17 #include <libecap/common/version.h>
18 #include <libecap/common/named_values.h>
19 #include "adaptation/ecap/Host.h" /* for protocol constants */
20 #include "adaptation/ecap/MessageRep.h"
21 #include "adaptation/ecap/XactionRep.h"
22 #include "base/TextException.h"
27 Adaptation::Ecap::HeaderRep::HeaderRep(HttpMsg
&aMessage
): theHeader(aMessage
.header
),
33 Adaptation::Ecap::HeaderRep::hasAny(const Name
&name
) const
35 const Http::HdrType squidId
= TranslateHeaderId(name
);
36 // XXX: optimize to remove getByName: we do not need the value here
37 return squidId
== Http::HdrType::OTHER
?
38 theHeader
.getByName(name
.image().c_str()).size() > 0:
39 (bool)theHeader
.has(squidId
);
42 Adaptation::Ecap::HeaderRep::Value
43 Adaptation::Ecap::HeaderRep::value(const Name
&name
) const
45 const Http::HdrType squidId
= TranslateHeaderId(name
);
46 const String value
= squidId
== Http::HdrType::OTHER
?
47 theHeader
.getByName(name
.image().c_str()) :
48 theHeader
.getStrOrList(squidId
);
49 return value
.size() > 0 ?
50 Value::FromTempString(value
.termedBuf()) : Value();
54 Adaptation::Ecap::HeaderRep::add(const Name
&name
, const Value
&value
)
56 const Http::HdrType squidId
= TranslateHeaderId(name
); // Http::HdrType::OTHER OK
57 HttpHeaderEntry
*e
= new HttpHeaderEntry(squidId
, name
.image().c_str(),
58 value
.toString().c_str());
59 theHeader
.addEntry(e
);
61 if (squidId
== Http::HdrType::CONTENT_LENGTH
)
62 theMessage
.content_length
= theHeader
.getInt64(Http::HdrType::CONTENT_LENGTH
);
66 Adaptation::Ecap::HeaderRep::removeAny(const Name
&name
)
68 const Http::HdrType squidId
= TranslateHeaderId(name
);
69 if (squidId
== Http::HdrType::OTHER
)
70 theHeader
.delByName(name
.image().c_str());
72 theHeader
.delById(squidId
);
74 if (squidId
== Http::HdrType::CONTENT_LENGTH
)
75 theMessage
.content_length
= theHeader
.getInt64(Http::HdrType::CONTENT_LENGTH
);
79 Adaptation::Ecap::HeaderRep::visitEach(libecap::NamedValueVisitor
&visitor
) const
81 HttpHeaderPos pos
= HttpHeaderInitPos
;
82 while (HttpHeaderEntry
*e
= theHeader
.getEntry(&pos
)) {
83 const Name
name(e
->name
.termedBuf()); // optimize: find std Names
84 name
.assignHostId(e
->id
);
85 visitor
.visit(name
, Value(e
->value
.rawBuf(), e
->value
.size()));
90 Adaptation::Ecap::HeaderRep::image() const
94 theMessage
.packInto(&mb
, true);
95 return Area::FromTempBuffer(mb
.content(), mb
.contentSize());
100 Adaptation::Ecap::HeaderRep::parse(const Area
&buf
)
102 Http::StatusCode error
;
103 Must(theMessage
.parse(buf
.start
, buf
.size
, true, &error
));
107 Adaptation::Ecap::HeaderRep::TranslateHeaderId(const Name
&name
)
109 if (name
.assignedHostId())
110 return static_cast<Http::HdrType
>(name
.hostId());
111 return Http::HdrType::OTHER
;
116 Adaptation::Ecap::FirstLineRep::FirstLineRep(HttpMsg
&aMessage
): theMessage(aMessage
)
121 Adaptation::Ecap::FirstLineRep::version() const
123 return libecap::Version(theMessage
.http_ver
.major
,
124 theMessage
.http_ver
.minor
);
128 Adaptation::Ecap::FirstLineRep::version(const libecap::Version
&aVersion
)
130 theMessage
.http_ver
.major
= aVersion
.majr
;
131 theMessage
.http_ver
.minor
= aVersion
.minr
;
135 Adaptation::Ecap::FirstLineRep::protocol() const
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
:
156 case AnyP::PROTO_HTCP
:
159 case AnyP::PROTO_CACHE_OBJECT
:
160 return protocolCacheObj
;
161 case AnyP::PROTO_ICY
:
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
:
170 case AnyP::PROTO_MAX
:
171 break; // should not happen
172 // no default to catch AnyP::PROTO_ additions
174 Must(false); // not reached
179 Adaptation::Ecap::FirstLineRep::protocol(const Name
&p
)
181 // TODO: what happens if we fail to translate some protocol?
182 theMessage
.http_ver
.protocol
= TranslateProtocolId(p
);
186 Adaptation::Ecap::FirstLineRep::TranslateProtocolId(const Name
&name
)
188 if (name
.assignedHostId())
189 return static_cast<AnyP::ProtocolType
>(name
.hostId());
190 return AnyP::PROTO_UNKNOWN
;
193 /* RequestHeaderRep */
195 Adaptation::Ecap::RequestLineRep::RequestLineRep(HttpRequest
&aMessage
):
196 FirstLineRep(aMessage
), theMessage(aMessage
)
201 Adaptation::Ecap::RequestLineRep::uri(const Area
&aUri
)
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
);
212 Adaptation::Ecap::RequestLineRep::Area
213 Adaptation::Ecap::RequestLineRep::uri() const
215 const SBuf
&fullUrl
= theMessage
.effectiveRequestUri();
216 // XXX: effectiveRequestUri() cannot return NULL or even empty string, some other problem?
217 Must(!fullUrl
.isEmpty());
218 // optimize: avoid copying by having an Area::Detail that locks theMessage
219 return Area::FromTempBuffer(fullUrl
.rawContent(), fullUrl
.length());
223 Adaptation::Ecap::RequestLineRep::method(const Name
&aMethod
)
225 if (aMethod
.assignedHostId()) {
226 const int id
= aMethod
.hostId();
227 Must(Http::METHOD_NONE
< id
&& id
< Http::METHOD_ENUM_END
);
228 Must(id
!= Http::METHOD_OTHER
);
229 theMessage
.method
= HttpRequestMethod(static_cast<Http::MethodType
>(id
));
231 const std::string
&image
= aMethod
.image();
232 theMessage
.method
.HttpRequestMethodXXX(image
.c_str());
236 Adaptation::Ecap::RequestLineRep::Name
237 Adaptation::Ecap::RequestLineRep::method() const
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
;
255 return Name(theMessage
.method
.image().toStdString());
260 Adaptation::Ecap::RequestLineRep::version() const
262 return FirstLineRep::version();
266 Adaptation::Ecap::RequestLineRep::version(const libecap::Version
&aVersion
)
268 FirstLineRep::version(aVersion
);
272 Adaptation::Ecap::RequestLineRep::protocol() const
274 return FirstLineRep::protocol();
278 Adaptation::Ecap::RequestLineRep::protocol(const Name
&p
)
280 FirstLineRep::protocol(p
);
285 Adaptation::Ecap::StatusLineRep::StatusLineRep(HttpReply
&aMessage
):
286 FirstLineRep(aMessage
), theMessage(aMessage
)
291 Adaptation::Ecap::StatusLineRep::statusCode(int code
)
293 theMessage
.sline
.set(theMessage
.sline
.version
, static_cast<Http::StatusCode
>(code
), nullptr);
297 Adaptation::Ecap::StatusLineRep::statusCode() const
299 // TODO: remove cast when possible
300 return static_cast<int>(theMessage
.sline
.status());
304 Adaptation::Ecap::StatusLineRep::reasonPhrase(const Area
&)
306 // Squid does not support external custom reason phrases so we have
307 // to just reset it (in case there was a custom internal reason set)
308 theMessage
.sline
.resetReason();
311 Adaptation::Ecap::StatusLineRep::Area
312 Adaptation::Ecap::StatusLineRep::reasonPhrase() const
314 return Area::FromTempString(std::string(theMessage
.sline
.reason()));
318 Adaptation::Ecap::StatusLineRep::version() const
320 return FirstLineRep::version();
324 Adaptation::Ecap::StatusLineRep::version(const libecap::Version
&aVersion
)
326 FirstLineRep::version(aVersion
);
330 Adaptation::Ecap::StatusLineRep::protocol() const
332 return FirstLineRep::protocol();
336 Adaptation::Ecap::StatusLineRep::protocol(const Name
&p
)
338 FirstLineRep::protocol(p
);
343 Adaptation::Ecap::BodyRep::BodyRep(const BodyPipe::Pointer
&aBody
): theBody(aBody
)
348 Adaptation::Ecap::BodyRep::tie(const BodyPipe::Pointer
&aBody
)
355 Adaptation::Ecap::BodyRep::BodySize
356 Adaptation::Ecap::BodyRep::bodySize() const
358 return (theBody
!= nullptr && theBody
->bodySizeKnown()) ? BodySize(theBody
->bodySize()) : BodySize();
363 Adaptation::Ecap::MessageRep::MessageRep(HttpMsg
*rawHeader
):
364 theMessage(rawHeader
), theFirstLineRep(NULL
),
365 theHeaderRep(NULL
), theBodyRep(NULL
)
367 Must(theMessage
.header
); // we do not want to represent a missing message
369 if (HttpRequest
*req
= dynamic_cast<HttpRequest
*>(theMessage
.header
))
370 theFirstLineRep
= new RequestLineRep(*req
);
371 else if (HttpReply
*rep
= dynamic_cast<HttpReply
*>(theMessage
.header
))
372 theFirstLineRep
= new StatusLineRep(*rep
);
374 Must(false); // unknown message header type
376 theHeaderRep
= new HeaderRep(*theMessage
.header
);
378 if (theMessage
.body_pipe
!= NULL
)
379 theBodyRep
= new BodyRep(theMessage
.body_pipe
);
382 Adaptation::Ecap::MessageRep::~MessageRep()
386 delete theFirstLineRep
;
389 libecap::shared_ptr
<libecap::Message
>
390 Adaptation::Ecap::MessageRep::clone() const
392 HttpMsg
*hdr
= theMessage
.header
->clone();
393 hdr
->body_pipe
= NULL
; // if any; TODO: remove pipe cloning from ::clone?
394 libecap::shared_ptr
<libecap::Message
> res(new MessageRep(hdr
));
396 // restore indication of a body if needed, but not the pipe
397 if (theMessage
.header
->body_pipe
!= NULL
)
404 Adaptation::Ecap::MessageRep::firstLine()
406 return *theFirstLineRep
;
409 const libecap::FirstLine
&
410 Adaptation::Ecap::MessageRep::firstLine() const
412 return *theFirstLineRep
;
416 Adaptation::Ecap::MessageRep::header()
418 return *theHeaderRep
;
421 const libecap::Header
&
422 Adaptation::Ecap::MessageRep::header() const
424 return *theHeaderRep
;
428 Adaptation::Ecap::MessageRep::body()
434 Adaptation::Ecap::MessageRep::addBody()
437 Must(!theMessage
.body_pipe
); // set in tieBody()
438 theBodyRep
= new BodyRep(NULL
);
442 Adaptation::Ecap::MessageRep::tieBody(Adaptation::Ecap::XactionRep
*x
)
444 Must(theBodyRep
!= NULL
); // addBody must be called first
445 Must(!theMessage
.header
->body_pipe
);
446 Must(!theMessage
.body_pipe
);
447 theMessage
.header
->body_pipe
= new BodyPipe(x
);
448 theMessage
.body_pipe
= theMessage
.header
->body_pipe
;
449 theBodyRep
->tie(theMessage
.body_pipe
);
452 const libecap::Body
*Adaptation::Ecap::MessageRep::body() const