2 * Copyright (C) 1996-2023 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 #ifndef SQUID_SRC_HTTP_STREAM_H
10 #define SQUID_SRC_HTTP_STREAM_H
12 #include "clientStreamForward.h"
13 #include "comm/forward.h"
14 #include "debug/Stream.h"
15 #include "error/Error.h"
16 #include "http/forward.h"
17 #include "log/forward.h"
18 #include "mem/forward.h"
19 #include "servers/forward.h"
20 #include "StoreIOBuffer.h"
22 #include "MessageBucket.h"
29 * The processing context for a single HTTP transaction (stream).
31 * A stream lifetime extends from directly after a request has been parsed
32 * off the client connection buffer, until the last byte of both request
33 * and reply payload (if any) have been written, or it is otherwise
34 * explicitly terminated.
36 * Streams self-register with the Http::Server Pipeline being managed by the
37 * Server for the connection on which the request was received.
39 * The socket level management and I/O is done by a Server which owns us.
40 * The scope of this objects control over a socket consists of the data
41 * buffer received from the Server with an initially unknown length.
42 * When that length is known it sets the end boundary of our access to the
45 * The individual processing actions are done by other Jobs which we start.
47 * When a stream is completed the finished() method needs to be called which
48 * will perform all cleanup and deregistration operations. If the reason for
49 * finishing is an error, then notifyIoError() needs to be called prior to
50 * the finished() method.
51 * The caller should follow finished() with a call to ConnStateData::kick()
52 * to resume processing of other transactions or I/O on the connection.
54 * Alternatively the initiateClose() method can be called to terminate the
55 * whole client connection and all other pending streams.
59 * When HTTP/1 pipeline is operating there may be multiple transactions using
60 * the client connection. Only the back() stream may read from the connection,
61 * and only the front() stream may write to it. A stream which needs to read
62 * or write to the connection but does not meet those criteria must be shifted
63 * to the deferred state.
66 * XXX: If an async call ends the ClientHttpRequest job, Http::Stream
67 * (and ConnStateData) may not know about it, leading to segfaults and
68 * assertions. This is difficult to fix
69 * because ClientHttpRequest lacks a good way to communicate its ongoing
70 * destruction back to the Http::Stream which pretends to "own" *http.
72 class Stream
: public RefCountable
74 MEMPROXY_CLASS(Stream
);
77 /// construct with HTTP/1.x details
78 Stream(const Comm::ConnectionPointer
&aConn
, ClientHttpRequest
*aReq
);
81 /// register this stream with the Server
82 void registerWithConn();
84 /// whether it is registered with a Server
85 bool connRegistered() const {return connRegistered_
;};
87 /// whether the reply has started being sent
88 bool startOfOutput() const;
90 /// update stream state after a write, may initiate more I/O
91 void writeComplete(size_t size
);
93 /// get more data to send
96 /// \return true if the HTTP request is for multiple ranges
97 bool multipartRangeRequest() const;
99 int64_t getNextRangeOffset() const;
100 bool canPackMoreRanges() const;
101 size_t lengthToSend(Range
<int64_t> const &available
) const;
103 clientStream_status_t
socketState();
105 /// send an HTTP reply message headers and maybe some initial payload
106 void sendStartOfMessage(HttpReply
*, StoreIOBuffer bodyData
);
107 /// send some HTTP reply message payload
108 void sendBody(StoreIOBuffer bodyData
);
109 /// update stream state when N bytes are being sent.
110 /// NP: Http1Server bytes actually not sent yet, just packed into a MemBuf ready
111 void noteSentBodyBytes(size_t);
113 /// add Range headers (if any) to the given HTTP reply message
114 void buildRangeHeader(HttpReply
*);
116 clientStreamNode
* getTail() const;
117 clientStreamNode
* getClientReplyContext() const;
119 ConnStateData
*getConn() const;
121 /// update state to reflect I/O error
122 void noteIoError(const Error
&, const LogTagsErrors
&);
124 /// cleanup when the transaction has finished. may destroy 'this'
127 /// terminate due to a send/write error (may continue reading)
128 void initiateClose(const char *reason
);
130 void deferRecipientForLater(clientStreamNode
*, HttpReply
*, StoreIOBuffer receivedData
);
132 public: // HTTP/1.x state data
134 Comm::ConnectionPointer clientConnection
; ///< details about the client connection socket
135 ClientHttpRequest
*http
; /* we pretend to own that Job */
137 char reqbuf
[HTTP_REQBUF_SZ
];
139 unsigned deferred
:1; ///< This is a pipelined request waiting for the current object to complete
140 unsigned parsed_ok
:1; ///< Was this parsed correctly?
143 bool mayUseConnection() const {return mayUseConnection_
;}
145 void mayUseConnection(bool aBool
) {
146 mayUseConnection_
= aBool
;
147 debugs(33, 3, "This " << this << " marked " << aBool
);
154 clientStreamNode
*node
;
156 StoreIOBuffer queuedBuffer
;
159 DeferredParams deferredparams
;
160 int64_t writtenToSocket
;
163 void prepareReply(HttpReply
*);
164 void packChunk(const StoreIOBuffer
&bodyData
, MemBuf
&);
165 void packRange(StoreIOBuffer
const &, MemBuf
*);
168 bool mayUseConnection_
; /* This request may use the connection. Don't read anymore requests for now */
169 bool connRegistered_
;
171 MessageBucket::Pointer writeQuotaHandler
; ///< response write limiter, if configured
177 #endif /* SQUID_SRC_HTTP_STREAM_H */