]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/adaptation/ecap/MessageRep.cc
12fb703efacf0fecb7484b3f066255c7d330cfb7
2 * DEBUG: section 93 eCAP Interface
5 #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/MessageRep.h"
13 #include "adaptation/ecap/XactionRep.h"
14 #include "adaptation/ecap/Host.h" /* for protocol constants */
15 #include "base/TextException.h"
19 Adaptation::Ecap::HeaderRep::HeaderRep(HttpMsg
&aMessage
): theHeader(aMessage
.header
),
25 Adaptation::Ecap::HeaderRep::hasAny(const Name
&name
) const
27 const http_hdr_type squidId
= TranslateHeaderId(name
);
28 // XXX: optimize to remove getByName: we do not need the value here
29 return squidId
== HDR_OTHER
?
30 theHeader
.getByName(name
.image().c_str()).size() > 0:
31 (bool)theHeader
.has(squidId
);
34 Adaptation::Ecap::HeaderRep::Value
35 Adaptation::Ecap::HeaderRep::value(const Name
&name
) const
37 const http_hdr_type squidId
= TranslateHeaderId(name
);
38 const String value
= squidId
== HDR_OTHER
?
39 theHeader
.getByName(name
.image().c_str()) :
40 theHeader
.getStrOrList(squidId
);
41 return value
.defined() ?
42 Value::FromTempString(value
.termedBuf()) : Value();
46 Adaptation::Ecap::HeaderRep::add(const Name
&name
, const Value
&value
)
48 const http_hdr_type squidId
= TranslateHeaderId(name
); // HDR_OTHER OK
49 HttpHeaderEntry
*e
= new HttpHeaderEntry(squidId
, name
.image().c_str(),
50 value
.toString().c_str());
51 theHeader
.addEntry(e
);
53 if (squidId
== HDR_CONTENT_LENGTH
)
54 theMessage
.content_length
= theHeader
.getInt64(HDR_CONTENT_LENGTH
);
58 Adaptation::Ecap::HeaderRep::removeAny(const Name
&name
)
60 const http_hdr_type squidId
= TranslateHeaderId(name
);
61 if (squidId
== HDR_OTHER
)
62 theHeader
.delByName(name
.image().c_str());
64 theHeader
.delById(squidId
);
66 if (squidId
== HDR_CONTENT_LENGTH
)
67 theMessage
.content_length
= theHeader
.getInt64(HDR_CONTENT_LENGTH
);
71 Adaptation::Ecap::HeaderRep::visitEach(libecap::NamedValueVisitor
&visitor
) const
73 HttpHeaderPos pos
= HttpHeaderInitPos
;
74 while (HttpHeaderEntry
*e
= theHeader
.getEntry(&pos
)) {
75 const Name
name(e
->name
.termedBuf()); // optimize: find std Names
76 name
.assignHostId(e
->id
);
77 visitor
.visit(name
, Value(e
->value
.rawBuf(), e
->value
.size()));
82 Adaptation::Ecap::HeaderRep::image() const
88 packerToMemInit(&p
, &mb
);
89 theMessage
.packInto(&p
, true);
91 return Area::FromTempBuffer(mb
.content(), mb
.contentSize());
96 Adaptation::Ecap::HeaderRep::parse(const Area
&buf
)
100 mb
.append(buf
.start
, buf
.size
);
102 Must(theMessage
.parse(&mb
, true, &error
));
106 Adaptation::Ecap::HeaderRep::TranslateHeaderId(const Name
&name
)
108 if (name
.assignedHostId())
109 return static_cast<http_hdr_type
>(name
.hostId());
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
.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_INTERNAL
:
162 return protocolInternal
;
163 case AnyP::PROTO_ICY
:
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
.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
;
194 /* RequestHeaderRep */
196 Adaptation::Ecap::RequestLineRep::RequestLineRep(HttpRequest
&aMessage
):
197 FirstLineRep(aMessage
), theMessage(aMessage
)
202 Adaptation::Ecap::RequestLineRep::uri(const Area
&aUri
)
204 // TODO: if method is not set, urlPath will assume it is not connect;
205 // Can we change urlParse API to remove the method parameter?
206 // TODO: optimize: urlPath should take constant URL buffer
207 char *buf
= xstrdup(aUri
.toString().c_str());
208 const bool ok
= urlParse(theMessage
.method
, buf
, &theMessage
);
213 Adaptation::Ecap::RequestLineRep::Area
214 Adaptation::Ecap::RequestLineRep::uri() const
216 const char *fullUrl
= urlCanonical(&theMessage
);
218 // optimize: avoid copying by having an Area::Detail that locks theMessage
219 return Area::FromTempBuffer(fullUrl
, strlen(fullUrl
));
223 Adaptation::Ecap::RequestLineRep::method(const Name
&aMethod
)
225 if (aMethod
.assignedHostId()) {
226 const int id
= aMethod
.hostId();
227 Must(METHOD_NONE
< id
&& id
< METHOD_ENUM_END
);
228 Must(id
!= METHOD_OTHER
);
229 theMessage
.method
= HttpRequestMethod(static_cast<_method_t
>(id
));
231 const std::string
&image
= aMethod
.image();
232 theMessage
.method
= HttpRequestMethod(image
.data(),
233 image
.data() + image
.size());
237 Adaptation::Ecap::RequestLineRep::Name
238 Adaptation::Ecap::RequestLineRep::method() const
240 switch (theMessage
.method
.id()) {
242 return libecap::methodGet
;
244 return libecap::methodPost
;
246 return libecap::methodPut
;
248 return libecap::methodHead
;
250 return libecap::methodConnect
;
252 return libecap::methodDelete
;
254 return libecap::methodTrace
;
256 return Name(theMessage
.method
.image());
261 Adaptation::Ecap::RequestLineRep::version() const
263 return FirstLineRep::version();
267 Adaptation::Ecap::RequestLineRep::version(const libecap::Version
&aVersion
)
269 FirstLineRep::version(aVersion
);
273 Adaptation::Ecap::RequestLineRep::protocol() const
275 return FirstLineRep::protocol();
279 Adaptation::Ecap::RequestLineRep::protocol(const Name
&p
)
281 FirstLineRep::protocol(p
);
287 Adaptation::Ecap::StatusLineRep::StatusLineRep(HttpReply
&aMessage
):
288 FirstLineRep(aMessage
), theMessage(aMessage
)
293 Adaptation::Ecap::StatusLineRep::statusCode(int code
)
295 // TODO: why is .status a enum? Do we not support unknown statuses?
296 theMessage
.sline
.status
= static_cast<http_status
>(code
);
300 Adaptation::Ecap::StatusLineRep::statusCode() const
302 // TODO: see statusCode(code) TODO above
303 return static_cast<int>(theMessage
.sline
.status
);
307 Adaptation::Ecap::StatusLineRep::reasonPhrase(const Area
&)
309 // Squid does not support custom reason phrases
310 theMessage
.sline
.reason
= NULL
;
313 Adaptation::Ecap::StatusLineRep::Area
314 Adaptation::Ecap::StatusLineRep::reasonPhrase() const
316 return theMessage
.sline
.reason
?
317 Area::FromTempString(std::string(theMessage
.sline
.reason
)) : Area();
321 Adaptation::Ecap::StatusLineRep::version() const
323 return FirstLineRep::version();
327 Adaptation::Ecap::StatusLineRep::version(const libecap::Version
&aVersion
)
329 FirstLineRep::version(aVersion
);
333 Adaptation::Ecap::StatusLineRep::protocol() const
335 return FirstLineRep::protocol();
339 Adaptation::Ecap::StatusLineRep::protocol(const Name
&p
)
341 FirstLineRep::protocol(p
);
346 Adaptation::Ecap::BodyRep::BodyRep(const BodyPipe::Pointer
&aBody
): theBody(aBody
)
351 Adaptation::Ecap::BodyRep::tie(const BodyPipe::Pointer
&aBody
)
358 Adaptation::Ecap::BodyRep::BodySize
359 Adaptation::Ecap::BodyRep::bodySize() const
361 return !theBody
? BodySize() : BodySize(theBody
->bodySize());
367 Adaptation::Ecap::MessageRep::MessageRep(HttpMsg
*rawHeader
):
368 theMessage(rawHeader
), theFirstLineRep(NULL
),
369 theHeaderRep(NULL
), theBodyRep(NULL
)
371 Must(theMessage
.header
); // we do not want to represent a missing message
373 if (HttpRequest
*req
= dynamic_cast<HttpRequest
*>(theMessage
.header
))
374 theFirstLineRep
= new RequestLineRep(*req
);
375 else if (HttpReply
*rep
= dynamic_cast<HttpReply
*>(theMessage
.header
))
376 theFirstLineRep
= new StatusLineRep(*rep
);
378 Must(false); // unknown message header type
380 theHeaderRep
= new HeaderRep(*theMessage
.header
);
382 if (theMessage
.body_pipe
!= NULL
)
383 theBodyRep
= new BodyRep(theMessage
.body_pipe
);
386 Adaptation::Ecap::MessageRep::~MessageRep()
390 delete theFirstLineRep
;
393 libecap::shared_ptr
<libecap::Message
>
394 Adaptation::Ecap::MessageRep::clone() const
396 HttpMsg
*hdr
= theMessage
.header
->clone();
397 hdr
->body_pipe
= NULL
; // if any; TODO: remove pipe cloning from ::clone?
398 libecap::shared_ptr
<libecap::Message
> res(new MessageRep(hdr
));
400 // restore indication of a body if needed, but not the pipe
401 if (theMessage
.header
->body_pipe
!= NULL
)
408 Adaptation::Ecap::MessageRep::firstLine()
410 return *theFirstLineRep
;
413 const libecap::FirstLine
&
414 Adaptation::Ecap::MessageRep::firstLine() const
416 return *theFirstLineRep
;
420 Adaptation::Ecap::MessageRep::header()
422 return *theHeaderRep
;
425 const libecap::Header
&
426 Adaptation::Ecap::MessageRep::header() const
428 return *theHeaderRep
;
432 Adaptation::Ecap::MessageRep::body()
438 Adaptation::Ecap::MessageRep::addBody()
441 Must(!theMessage
.body_pipe
); // set in tieBody()
442 theBodyRep
= new BodyRep(NULL
);
446 Adaptation::Ecap::MessageRep::tieBody(Adaptation::Ecap::XactionRep
*x
)
448 Must(theBodyRep
!= NULL
); // addBody must be called first
449 Must(!theMessage
.header
->body_pipe
);
450 Must(!theMessage
.body_pipe
);
451 theMessage
.header
->body_pipe
= new BodyPipe(x
);
452 theMessage
.body_pipe
= theMessage
.header
->body_pipe
;
453 theBodyRep
->tie(theMessage
.body_pipe
);
456 const libecap::Body
*Adaptation::Ecap::MessageRep::body() const