Http1Server.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2022 The Squid Software Foundation and contributors
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.
7 */
8
9/* DEBUG: section 33 Client-side Routines */
10
11#include "squid.h"
12#include "acl/FilledChecklist.h"
13#include "client_side.h"
14#include "client_side_reply.h"
15#include "client_side_request.h"
16#include "comm/Write.h"
18#include "http/Stream.h"
19#include "HttpHeaderTools.h"
20#include "servers/Http1Server.h"
21#include "SquidConfig.h"
22#include "Store.h"
23#include "tunnel.h"
24
26
27Http::One::Server::Server(const MasterXaction::Pointer &xact, bool beHttpsServer):
28 AsyncJob("Http1::Server"),
29 ConnStateData(xact),
30 isHttpsServer(beHttpsServer)
31{
32}
33
34time_t
36{
38}
39
40void
42{
44
45 // XXX: Until we create an HttpsServer class, use this hack to allow old
46 // client_side.cc code to manipulate ConnStateData object directly
47 if (isHttpsServer) {
48 postHttpsAccept();
49 return;
50 }
51
53 AsyncCall::Pointer timeoutCall = JobCallback(33, 5,
54 TimeoutDialer, this, Http1::Server::requestTimeout);
55 commSetConnTimeout(clientConnection, Config.Timeout.request_start_timeout, timeoutCall);
56 readSomeData();
57}
58
59void
61{
62 if (!handleRequestBodyData())
63 return;
64
65 // too late to read more body
66 if (!isOpen() || stoppedReceiving())
67 return;
68
69 readSomeData();
70}
71
74{
75 // reset because the protocol may have changed if this is the first request
76 // and because we never bypass parsing failures of N+1st same-proto request
77 preservingClientData_ = shouldPreserveClientData();
78
79 // parser is incremental. Generate new parser state if we,
80 // a) do not have one already
81 // b) have completed the previous request parsing already
82 if (!parser_ || !parser_->needsMoreData())
83 parser_ = new Http1::RequestParser(preservingClientData_);
84
85 /* Process request */
86 Http::Stream *context = parseHttpRequest(parser_);
87
88 return context;
89}
90
92
93bool
95{
97 ClientHttpRequest *http = context->http;
98 if (context->flags.parsed_ok == 0) {
99 debugs(33, 2, "Invalid Request");
100 // determine which error page templates to use for specific parsing errors
101 err_type errPage = ERR_INVALID_REQ;
102 switch (parser_->parseStatusCode) {
104 // fall through to next case
106 errPage = ERR_TOO_BIG;
107 break;
109 errPage = ERR_UNSUP_REQ;
110 break;
112 errPage = ERR_UNSUP_HTTPVERSION;
113 break;
114 default:
115 if (parser_->method() == METHOD_NONE || parser_->requestUri().length() == 0)
116 // no method or url parsed, probably is wrong protocol
117 errPage = ERR_PROTOCOL_UNKNOWN;
118 // else use default ERR_INVALID_REQ set above.
119 break;
120 }
121 // setReplyToError() requires log_uri
122 // must be already initialized via ConnStateData::abortRequestParsing()
123 assert(http->log_uri);
124
125 const char * requestErrorBytes = inBuf.c_str();
126 if (!tunnelOnError(errPage)) {
127 setReplyError(context, request, errPage, parser_->parseStatusCode, requestErrorBytes);
128 // HttpRequest object not build yet, there is no reason to call
129 // clientProcessRequestFinished method
130 }
131
132 return false;
133 }
134
135 // TODO: move URL parse into Http Parser and INVALID_URL into the above parse error handling
136 const auto mx = MasterXaction::MakePortful(port);
137 mx->tcpClient = clientConnection;
138 request = HttpRequest::FromUrlXXX(http->uri, mx, parser_->method());
139 if (!request) {
140 debugs(33, 5, "Invalid URL: " << http->uri);
141 // setReplyToError() requires log_uri
142 http->setLogUriToRawUri(http->uri, parser_->method());
143
144 const char * requestErrorBytes = inBuf.c_str();
145 if (!tunnelOnError(ERR_INVALID_URL)) {
146 setReplyError(context, request, ERR_INVALID_URL, Http::scBadRequest, requestErrorBytes);
147 // HttpRequest object not build yet, there is no reason to call
148 // clientProcessRequestFinished method
149 }
150 return false;
151 }
152
153 /* RFC 2616 section 10.5.6 : handle unsupported HTTP major versions cleanly. */
154 /* We currently only support 0.9, 1.0, 1.1 properly */
155 /* TODO: move HTTP-specific processing into servers/HttpServer and such */
156 if ( (parser_->messageProtocol().major == 0 && parser_->messageProtocol().minor != 9) ||
157 (parser_->messageProtocol().major > 1) ) {
158
159 debugs(33, 5, "Unsupported HTTP version discovered. :\n" << parser_->messageProtocol());
160 // setReplyToError() requires log_uri
161 http->setLogUriToRawUri(http->uri, parser_->method());
162
163 const char * requestErrorBytes = nullptr; //HttpParserHdrBuf(parser_);
164 if (!tunnelOnError(ERR_UNSUP_HTTPVERSION)) {
165 setReplyError(context, request, ERR_UNSUP_HTTPVERSION, Http::scHttpVersionNotSupported, requestErrorBytes);
167 }
168 return false;
169 }
170
171 /* compile headers */
172 if (parser_->messageProtocol().major >= 1 && !request->parseHeader(*parser_.getRaw())) {
173 debugs(33, 5, "Failed to parse request headers:\n" << parser_->mimeHeader());
174 // setReplyToError() requires log_uri
175 http->setLogUriToRawUri(http->uri, parser_->method());
176 const char * requestErrorBytes = nullptr; //HttpParserHdrBuf(parser_);
177 if (!tunnelOnError(ERR_INVALID_REQ)) {
178 setReplyError(context, request, ERR_INVALID_REQ, Http::scBadRequest, requestErrorBytes);
180 }
181 return false;
182 }
183
184 // when absolute-URI is provided Host header should be ignored. However
185 // some code still uses Host directly so normalize it using the previously
186 // sanitized URL authority value.
187 // For now preserve the case where Host is completely absent. That matters.
188 if (const auto x = request->header.delById(Http::HOST)) {
189 debugs(33, 5, "normalize " << x << " Host header using " << request->url.authority());
190 SBuf tmp(request->url.authority());
191 request->header.putStr(Http::HOST, tmp.c_str());
192 }
193
194 // TODO: We fill request notes here until we find a way to verify whether
195 // no ACL checking is performed before ClientHttpRequest::doCallouts().
196 if (hasNotes()) {
197 assert(!request->hasNotes());
198 request->notes()->append(notes().getRaw());
199 }
200
201 http->initRequest(request.getRaw());
202
203 return true;
204}
205
206void
207Http::One::Server::setReplyError(Http::StreamPointer &context, HttpRequest::Pointer &request, err_type requestError, Http::StatusCode errStatusCode, const char *requestErrorBytes)
208{
209 quitAfterError(request.getRaw());
210 if (!context->connRegistered()) {
211 debugs(33, 2, "Client stream deregister it self, nothing to do");
212 clientConnection->close();
213 return;
214 }
215 clientStreamNode *node = context->getClientReplyContext();
216 clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw());
217 assert (repContext);
218
219 repContext->setReplyToError(requestError, errStatusCode, context->http->uri, this, nullptr, requestErrorBytes, nullptr);
220
221 assert(context->http->out.offset == 0);
222 context->pullData();
223}
224
225void
227{
228 debugs(33, 5, "Body Continuation written");
229 clientProcessRequest(this, parser_, context.getRaw());
230}
231
232int
234{
235 const auto context = pipeline.back();
236 const auto request = (context && context->http) ? context->http->request : nullptr;
237 if (request && request->header.has(Http::HdrType::UPGRADE))
238 return 0;
239
241}
242
243void
245{
246 if (!buildHttpRequest(context))
247 return;
248
249 ClientHttpRequest *http = context->http;
251
252 if (request->header.has(Http::HdrType::EXPECT)) {
253 const String expect = request->header.getList(Http::HdrType::EXPECT);
254 const bool supportedExpect = (expect.caseCmp("100-continue") == 0);
255 if (!supportedExpect) {
256 clientStreamNode *node = context->getClientReplyContext();
257 quitAfterError(request.getRaw());
258 // setReplyToError() requires log_uri
259 assert(http->log_uri);
260 clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw());
261 assert (repContext);
263 this, request.getRaw(), nullptr, nullptr);
264 assert(context->http->out.offset == 0);
265 context->pullData();
267 return;
268 }
269
271 ACLFilledChecklist bodyContinuationCheck(Config.accessList.forceRequestBodyContinuation, request.getRaw(), nullptr);
272 bodyContinuationCheck.al = http->al;
273 bodyContinuationCheck.syncAle(request.getRaw(), http->log_uri);
274 if (bodyContinuationCheck.fastCheck().allowed()) {
275 debugs(33, 5, "Body Continuation forced");
276 request->forcedBodyContinuation = true;
277 //sendControlMsg
280
282 const AsyncCall::Pointer cb = asyncCall(11, 3, "Http1::Server::proceedAfterBodyContinuation", CbDialer(this, &Http1::Server::proceedAfterBodyContinuation, Http::StreamPointer(context)));
283 sendControlMsg(HttpControlMsg(rep, cb));
284 return;
285 }
286 }
287 }
288 clientProcessRequest(this, parser_, context.getRaw());
289}
290
291void
293{
295 stopReceiving("virgin request body consumer aborted"); // closes ASAP
296}
297
298void
300{
301 // the caller guarantees that we are dealing with the current context only
302 Http::StreamPointer context = pipeline.front();
303 Must(context != nullptr);
304 const ClientHttpRequest *http = context->http;
305 Must(http != nullptr);
306
307 // After sending Transfer-Encoding: chunked (at least), always send
308 // the last-chunk if there was no error, ignoring responseFinishedOrFailed.
309 const bool mustSendLastChunk = http->request->flags.chunkedReply &&
310 !http->request->flags.streamError &&
312 !context->startOfOutput();
313 const bool responseFinishedOrFailed = !rep &&
314 !receivedData.data &&
315 !receivedData.length;
316 if (responseFinishedOrFailed && !mustSendLastChunk) {
317 context->writeComplete(0);
318 return;
319 }
320
321 if (!context->startOfOutput()) {
322 context->sendBody(receivedData);
323 return;
324 }
325
326 assert(rep);
327 context->sendStartOfMessage(rep, receivedData);
328}
329
330bool
332{
333 Http::StreamPointer context = pipeline.front();
334 Must(context != nullptr);
335
336 // Ignore this late control message if we have started sending a
337 // reply to the user already (e.g., after an error).
338 if (context->reply) {
339 debugs(11, 2, "drop 1xx made late by " << context->reply);
340 return false;
341 }
342
343 const ClientHttpRequest *http = context->http;
344
345 // remember Upgrade header; removeHopByHopEntries() will remove it
346 String upgradeHeader;
347 const auto switching = (rep->sline.status() == Http::scSwitchingProtocols);
348 if (switching)
349 upgradeHeader = rep->header.getList(Http::HdrType::UPGRADE);
350
351 // apply selected clientReplyContext::buildReplyHeader() mods
352 // it is not clear what headers are required for control messages
354 // paranoid: ContentLengthInterpreter has cleaned non-generated replies
356
357 if (switching && /* paranoid: */ upgradeHeader.size()) {
358 rep->header.putStr(Http::HdrType::UPGRADE, upgradeHeader.termedBuf());
359 rep->header.putStr(Http::HdrType::CONNECTION, "upgrade");
360 // keep-alive is redundant, breaks some 101 (Switching Protocols) recipients
361 } else {
362 rep->header.putStr(Http::HdrType::CONNECTION, "keep-alive");
363 }
364
365 httpHdrMangleList(&rep->header, http->request, http->al, ROR_REPLY);
366
367 MemBuf *mb = rep->pack();
368
369 debugs(11, 2, "HTTP Client " << clientConnection);
370 debugs(11, 2, "HTTP Client CONTROL MSG:\n---------\n" << mb->buf << "\n----------");
371
372 Comm::Write(clientConnection, mb, call);
373
374 delete mb;
375 return true;
376}
377
378void
380{
381 const auto context = pipeline.front();
382 assert(context);
383 const auto http = context->http;
384 assert(http);
385 assert(http->request);
386
387 stopReading();
388 Must(!writer);
389
390 switchToTunnel(http->request, clientConnection,
391 server.connection(), server.preReadServerBytes);
392}
393
396{
397 return new Http1::Server(xact, false);
398}
399
402{
403 return new Http1::Server(xact, true);
404}
405
AsyncCall * asyncCall(int aDebugSection, int aDebugLevel, const char *aName, const Dialer &aDialer)
Definition: AsyncCall.h:154
#define JobCallback(dbgSection, dbgLevel, Dialer, job, method)
Convenience macro to create a Dialer-based job callback.
Definition: AsyncJobCalls.h:69
void clientProcessRequestFinished(ConnStateData *conn, const HttpRequest::Pointer &request)
CBDATA_NAMESPACED_CLASS_INIT(Http1, Server)
void httpHdrMangleList(HttpHeader *l, HttpRequest *request, const AccessLogEntryPointer &al, req_or_rep_t req_or_rep)
@ ROR_REPLY
class SquidConfig Config
Definition: SquidConfig.cc:12
#define Must(condition)
Definition: TextException.h:71
int conn
the current server connection FD
Definition: Transport.cc:26
#define assert(EX)
Definition: assert.h:19
static char server[MAXLINE]
Acl::Answer const & fastCheck()
Definition: Checklist.cc:332
virtual void syncAle(HttpRequest *adaptedRequest, const char *logUri) const
assigns uninitialized adapted_request and url ALE components
AccessLogEntry::Pointer al
info for the future access.log, and external ACL
bool allowed() const
Definition: Acl.h:150
HttpRequest *const request
void initRequest(HttpRequest *)
void setLogUriToRawUri(const char *, const HttpRequestMethod &)
StoreEntry * storeEntry() const
const AccessLogEntry::Pointer al
access.log entry
noteTakeServerConnectionControl() callback parameter
Definition: client_side.h:214
virtual int pipelinePrefetchMax() const
returning N allows a pipeline of 1+N requests (see pipeline_prefetch)
virtual void noteBodyConsumerAborted(BodyPipe::Pointer)=0
virtual void start()
called by AsyncStart; do not call directly
void requestTimeout(const CommTimeoutCbParams &params)
bundles HTTP 1xx reply and the "successfully forwarded" callback
void removeHopByHopEntries()
Definition: HttpHeader.cc:1707
void putStr(Http::HdrType id, const char *str)
Definition: HttpHeader.cc:1028
String getList(Http::HdrType id) const
Definition: HttpHeader.cc:818
Http::StatusLine sline
Definition: HttpReply.h:56
MemBuf * pack() const
Definition: HttpReply.cc:110
void removeIrrelevantContentLength()
Some response status codes prohibit sending Content-Length (RFC 7230 section 3.3.2).
Definition: HttpReply.cc:607
static HttpRequest * FromUrlXXX(const char *url, const MasterXaction::Pointer &, const HttpRequestMethod &method=Http::METHOD_GET)
Definition: HttpRequest.cc:528
RequestFlags flags
Definition: HttpRequest.h:141
HttpHeader header
Definition: Message.h:74
Manages a connection from an HTTP/1 or HTTP/0.9 client.
Definition: Http1Server.h:21
virtual Http::Stream * parseOneRequest()
Definition: Http1Server.cc:73
virtual void noteBodyConsumerAborted(BodyPipe::Pointer)
Definition: Http1Server.cc:292
virtual void noteTakeServerConnectionControl(ServerConnectionContext)
Definition: Http1Server.cc:379
virtual void handleReply(HttpReply *rep, StoreIOBuffer receivedData)
Definition: Http1Server.cc:299
Server(const MasterXaction::Pointer &xact, const bool beHttpsServer)
Definition: Http1Server.cc:27
void proceedAfterBodyContinuation(Http::StreamPointer context)
Definition: Http1Server.cc:226
bool buildHttpRequest(Http::StreamPointer &context)
Definition: Http1Server.cc:94
void setReplyError(Http::StreamPointer &context, HttpRequest::Pointer &request, err_type requestError, Http::StatusCode errStatusCode, const char *requestErrorBytes)
Definition: Http1Server.cc:207
virtual void start()
called by AsyncStart; do not call directly
Definition: Http1Server.cc:41
virtual void noteMoreBodySpaceAvailable(BodyPipe::Pointer)
Definition: Http1Server.cc:60
virtual void processParsedRequest(Http::StreamPointer &context)
start processing a freshly parsed request
Definition: Http1Server.cc:244
virtual time_t idleTimeout() const
timeout to use when waiting for the next request
Definition: Http1Server.cc:35
virtual int pipelinePrefetchMax() const
returning N allows a pipeline of 1+N requests (see pipeline_prefetch)
Definition: Http1Server.cc:233
virtual bool writeControlMsgAndCall(HttpReply *rep, AsyncCall::Pointer &call)
handle a control message received by context from a peer and call back
Definition: Http1Server.cc:331
void set(const AnyP::ProtocolVersion &newVersion, Http::StatusCode newStatus, const char *newReason=nullptr)
Definition: StatusLine.cc:35
Http::StatusCode status() const
retrieve the status code for this status line
Definition: StatusLine.h:45
static Pointer MakePortful(const AnyP::PortCfgPointer &aPort)
Definition: MasterXaction.h:54
Definition: MemBuf.h:24
char * buf
Definition: MemBuf.h:134
C * getRaw() const
Definition: RefCount.h:80
bool chunkedReply
Definition: RequestFlags.h:90
bool streamError
Definition: RequestFlags.h:92
Definition: SBuf.h:94
const char * c_str()
Definition: SBuf.cc:516
Definition: Server.h:30
time_t request_start_timeout
Definition: SquidConfig.h:123
struct SquidConfig::@112 accessList
time_t clientIdlePconn
Definition: SquidConfig.h:117
struct SquidConfig::@98 Timeout
acl_access * forceRequestBodyContinuation
Definition: SquidConfig.h:402
uint16_t flags
Definition: Store.h:232
char const * termedBuf() const
Definition: SquidString.h:92
int caseCmp(char const *) const
Definition: String.cc:285
size_type size() const
Definition: SquidString.h:73
void setReplyToError(err_type, Http::StatusCode, char const *, const ConnStateData *, HttpRequest *, const char *, Auth::UserRequest::Pointer)
builds error using clientBuildError() and calls setReplyToError() below
void clientProcessRequest(ConnStateData *conn, const Http1::RequestParserPointer &hp, Http::Stream *context)
int commSetConnTimeout(const Comm::ConnectionPointer &conn, int timeout, AsyncCall::Pointer &callback)
Definition: comm.cc:589
bool isOpen(const int fd)
Definition: comm.cc:87
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:196
#define EBIT_TEST(flag, bit)
Definition: defines.h:69
static int port
Definition: ldap_backend.cc:70
@ ENTRY_BAD_LENGTH
Definition: enums.h:114
err_type
Definition: forward.h:14
@ ERR_PROTOCOL_UNKNOWN
Definition: forward.h:73
@ ERR_UNSUP_REQ
Definition: forward.h:44
@ ERR_INVALID_URL
Definition: forward.h:45
@ ERR_TOO_BIG
Definition: forward.h:40
@ ERR_UNSUP_HTTPVERSION
Definition: forward.h:42
@ ERR_INVALID_REQ
Definition: forward.h:43
void Write(const Comm::ConnectionPointer &conn, const char *buf, int size, AsyncCall::Pointer &callback, FREE *free_func)
Definition: Write.cc:33
ConnStateData * NewServer(const MasterXaction::Pointer &xact)
create a new HTTP connection handler; never returns NULL
Definition: Http1Server.cc:395
StatusCode
Definition: StatusCode.h:20
@ scUriTooLong
Definition: StatusCode.h:58
@ scHttpVersionNotSupported
Definition: StatusCode.h:76
@ scBadRequest
Definition: StatusCode.h:44
@ scExpectationFailed
Definition: StatusCode.h:61
@ scMethodNotAllowed
Definition: StatusCode.h:49
@ scContinue
Definition: StatusCode.h:22
@ scSwitchingProtocols
Definition: StatusCode.h:23
@ scRequestHeaderFieldsTooLarge
Definition: StatusCode.h:69
@ METHOD_NONE
Definition: MethodType.h:22
AnyP::ProtocolVersion ProtocolVersion(unsigned int aMajor, unsigned int aMinor)
HTTP version label information.
ConnStateData * NewServer(const MasterXaction::Pointer &xact)
create a new HTTPS connection handler; never returns NULL
Definition: Http1Server.cc:401
char * url
Definition: tcp-banger2.c:114
Definition: parse.c:104
struct _request * request(char *urlin)
Definition: tcp-banger2.c:291
void switchToTunnel(HttpRequest *request, const Comm::ConnectionPointer &clientConn, const Comm::ConnectionPointer &srvConn, const SBuf &preReadServerData)
Definition: tunnel.cc:1462

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors