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