FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/core/http/ChunkProxy.cpp at main · ssh352/rstudio · GitHub
ssh352
/
rstudio
Public
forked from
rstudio/rstudio
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
rstudio
/
src
/
cpp
/
core
/
http
/
ChunkProxy.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
180 lines (152 loc) · 4.87 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
core
/
http
/
ChunkProxy.cpp
Copy path
File metadata and controls
180 lines (152 loc) · 4.87 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* ChunkProxy.cpp
*
* Copyright (C) 2022 by Posit Software, PBC
*
* Unless you have received this program directly from Posit Software pursuant
* to the terms of a commercial license agreement with Posit Software, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
#
include
<
core/http/ChunkProxy.hpp
>
#
include
<
core/http/Util.hpp
>
using
namespace
boost
::placeholders
;
namespace
rstudio
{
namespace
core
{
namespace
http
{
namespace
{
bool
isLastChunk
(
const
std::string& chunk)
{
return
chunk ==
"
0
\r\n\r\n
"
;
}
}
//
anonymous namespace
ChunkProxy::ChunkProxy
(
const
boost::shared_ptr<AsyncConnection>& pClientConnection,
uint64_t
maxBufferSize) :
pClientConnection_
(pClientConnection),
maxBufferSize_
(maxBufferSize),
wroteHeaders_
(
false
),
currentBufferSize_
(
0
),
bufferFull_
(
false
)
{
}
void
ChunkProxy::proxy
(
const
boost::shared_ptr<IAsyncClient>& pServerConnection)
{
pServerConnection_ = pServerConnection;
pServerConnection_->
setChunkHandler
(
boost::bind
(&ChunkProxy::queueChunk,
shared_from_this
(),
_1, _2));
}
bool
ChunkProxy::queueChunk
(
const
http::Response& response,
const
std::string& chunk)
{
LOCK_MUTEX
(mutex_)
{
if
(currentBufferSize_ + chunk.
size
() > maxBufferSize_)
{
bufferFull_ =
true
;
//
we are temporarily out of space and cannot buffer any more chunks
//
until more data is written to the outgoing (client) connection
//
signal to connection to stop reading new data, and redeliver this chunk
//
when we have space for it
return
false
;
}
//
queue the chunk
std::string formattedChunk =
http::util::formatMessageAsHttpChunk
(chunk);
currentBufferSize_ += formattedChunk.
size
();
writeBuffer_.
emplace
(
std::move
(formattedChunk));
if
(!wroteHeaders_)
{
//
write the response headers and first chunk
http::Response& resp = pClientConnection_->
response
();
resp.
assign
(response);
pClientConnection_->
writeResponseHeaders
(
boost::bind
(&ChunkProxy::onHeadersWrote,
shared_from_this
(),
boost::asio::placeholders::error));
wroteHeaders_ =
true
;
}
else
{
if
(writeBuffer_.
size
() ==
1
)
{
//
we're the only chunk in the buffer, so we need to initiate a write
writeChunk
();
}
}
}
END_LOCK_MUTEX
return
true
;
}
void
ChunkProxy::onHeadersWrote
(
const
boost::system::error_code& ec)
{
if
(
handleError
(ec))
return
;
LOCK_MUTEX
(mutex_)
{
//
write the first chunk
writeChunk
();
}
END_LOCK_MUTEX
}
void
ChunkProxy::writeChunk
()
{
if
(writeBuffer_.
empty
())
{
if
(bufferFull_)
{
//
we previously hit a full buffer condition
//
inform the connection that we are ready to process chunks again
bufferFull_ =
false
;
pServerConnection_->
resumeChunkProcessing
();
}
return
;
}
const
std::string& chunk = writeBuffer_.
front
();
boost::asio::const_buffers_1
buffer
(chunk.
c_str
(), chunk.
size
());
pClientConnection_->
asyncWrite
(buffer,
boost::bind
(&ChunkProxy::onChunkWrote,
shared_from_this
(),
boost::asio::placeholders::error));
}
void
ChunkProxy::onChunkWrote
(
const
boost::system::error_code& ec)
{
if
(
handleError
(ec))
return
;
LOCK_MUTEX
(mutex_)
{
const
std::string& chunk = writeBuffer_.
front
();
bool
lastChunk =
isLastChunk
(chunk);
currentBufferSize_ -= writeBuffer_.
front
().
size
();
writeBuffer_.
pop
();
if
(lastChunk)
{
//
we wrote the last chunk - close connections
pClientConnection_->
close
();
pServerConnection_->
close
();
return
;
}
//
keep writing any queued chunks until we're empty
writeChunk
();
}
END_LOCK_MUTEX
}
bool
ChunkProxy::handleError
(
const
boost::system::error_code& ec)
{
if
(ec)
{
Error
error
(ec,
ERROR_LOCATION
);
if
(!
http::isConnectionTerminatedError
(error))
LOG_ERROR
(error);
//
close both connections to stop all data transfer
pClientConnection_->
close
();
pServerConnection_->
close
();
return
true
;
}
return
false
;
}
}
//
namespace http
}
//
namespace core
}
//
namespace rstudio
Back
|
FazBrowse Home
|
New Git URL