| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/tools/quic/quic_simple_server_stream.h" | 5 #include "net/tools/quic/quic_simple_server_stream.h" |
| 6 | 6 |
| 7 #include <list> | 7 #include <list> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "net/quic/core/quic_flags.h" | 10 #include "net/quic/core/quic_flags.h" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 SendErrorResponse(); | 54 SendErrorResponse(); |
| 55 } | 55 } |
| 56 | 56 |
| 57 void QuicSimpleServerStream::OnDataAvailable() { | 57 void QuicSimpleServerStream::OnDataAvailable() { |
| 58 while (HasBytesToRead()) { | 58 while (HasBytesToRead()) { |
| 59 struct iovec iov; | 59 struct iovec iov; |
| 60 if (GetReadableRegions(&iov, 1) == 0) { | 60 if (GetReadableRegions(&iov, 1) == 0) { |
| 61 // No more data to read. | 61 // No more data to read. |
| 62 break; | 62 break; |
| 63 } | 63 } |
| 64 QUIC_DVLOG(1) << "Processed " << iov.iov_len << " bytes for stream " | 64 QUIC_DVLOG(1) << "Stream " << id() << " processed " << iov.iov_len |
| 65 << id(); | 65 << " bytes."; |
| 66 body_.append(static_cast<char*>(iov.iov_base), iov.iov_len); | 66 body_.append(static_cast<char*>(iov.iov_base), iov.iov_len); |
| 67 | 67 |
| 68 if (content_length_ >= 0 && | 68 if (content_length_ >= 0 && |
| 69 body_.size() > static_cast<uint64_t>(content_length_)) { | 69 body_.size() > static_cast<uint64_t>(content_length_)) { |
| 70 QUIC_DVLOG(1) << "Body size (" << body_.size() << ") > content length (" | 70 QUIC_DVLOG(1) << "Body size (" << body_.size() << ") > content length (" |
| 71 << content_length_ << ")."; | 71 << content_length_ << ")."; |
| 72 SendErrorResponse(); | 72 SendErrorResponse(); |
| 73 return; | 73 return; |
| 74 } | 74 } |
| 75 MarkConsumed(iov.iov_len); | 75 MarkConsumed(iov.iov_len); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 93 void QuicSimpleServerStream::PushResponse( | 93 void QuicSimpleServerStream::PushResponse( |
| 94 SpdyHeaderBlock push_request_headers) { | 94 SpdyHeaderBlock push_request_headers) { |
| 95 if (id() % 2 != 0) { | 95 if (id() % 2 != 0) { |
| 96 QUIC_BUG << "Client initiated stream shouldn't be used as promised stream."; | 96 QUIC_BUG << "Client initiated stream shouldn't be used as promised stream."; |
| 97 return; | 97 return; |
| 98 } | 98 } |
| 99 // Change the stream state to emulate a client request. | 99 // Change the stream state to emulate a client request. |
| 100 request_headers_ = std::move(push_request_headers); | 100 request_headers_ = std::move(push_request_headers); |
| 101 content_length_ = 0; | 101 content_length_ = 0; |
| 102 QUIC_DVLOG(1) << "Stream " << id() | 102 QUIC_DVLOG(1) << "Stream " << id() |
| 103 << ": Ready to receive server push response."; | 103 << " ready to receive server push response."; |
| 104 | 104 |
| 105 // Set as if stream decompresed the headers and received fin. | 105 // Set as if stream decompresed the headers and received fin. |
| 106 QuicSpdyStream::OnInitialHeadersComplete(/*fin=*/true, 0, QuicHeaderList()); | 106 QuicSpdyStream::OnInitialHeadersComplete(/*fin=*/true, 0, QuicHeaderList()); |
| 107 } | 107 } |
| 108 | 108 |
| 109 void QuicSimpleServerStream::SendResponse() { | 109 void QuicSimpleServerStream::SendResponse() { |
| 110 if (request_headers_.empty()) { | 110 if (request_headers_.empty()) { |
| 111 QUIC_DVLOG(1) << "Request headers empty."; | 111 QUIC_DVLOG(1) << "Request headers empty."; |
| 112 SendErrorResponse(); | 112 SendErrorResponse(); |
| 113 return; | 113 return; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 bool is_redirection = response_code / 100 == 3; | 181 bool is_redirection = response_code / 100 == 3; |
| 182 if (response_code != 200 && !is_redirection) { | 182 if (response_code != 200 && !is_redirection) { |
| 183 QUIC_LOG(WARNING) << "Response to server push request " << request_url | 183 QUIC_LOG(WARNING) << "Response to server push request " << request_url |
| 184 << " result in response code " << response_code; | 184 << " result in response code " << response_code; |
| 185 Reset(QUIC_STREAM_CANCELLED); | 185 Reset(QUIC_STREAM_CANCELLED); |
| 186 return; | 186 return; |
| 187 } | 187 } |
| 188 } | 188 } |
| 189 std::list<QuicHttpResponseCache::ServerPushInfo> resources = | 189 std::list<QuicHttpResponseCache::ServerPushInfo> resources = |
| 190 response_cache_->GetServerPushResources(request_url); | 190 response_cache_->GetServerPushResources(request_url); |
| 191 QUIC_DVLOG(1) << "Found " << resources.size() << " push resources for stream " | 191 QUIC_DVLOG(1) << "Stream " << id() << " found " << resources.size() |
| 192 << id(); | 192 << " push resources."; |
| 193 | 193 |
| 194 if (!resources.empty()) { | 194 if (!resources.empty()) { |
| 195 QuicSimpleServerSession* session = | 195 QuicSimpleServerSession* session = |
| 196 static_cast<QuicSimpleServerSession*>(spdy_session()); | 196 static_cast<QuicSimpleServerSession*>(spdy_session()); |
| 197 session->PromisePushResources(request_url, resources, id(), | 197 session->PromisePushResources(request_url, resources, id(), |
| 198 request_headers_); | 198 request_headers_); |
| 199 } | 199 } |
| 200 | 200 |
| 201 QUIC_DVLOG(1) << "Sending response for stream " << id(); | 201 QUIC_DVLOG(1) << "Stream " << id() << " sending response."; |
| 202 SendHeadersAndBodyAndTrailers(response->headers().Clone(), response->body(), | 202 SendHeadersAndBodyAndTrailers(response->headers().Clone(), response->body(), |
| 203 response->trailers().Clone()); | 203 response->trailers().Clone()); |
| 204 } | 204 } |
| 205 | 205 |
| 206 void QuicSimpleServerStream::SendNotFoundResponse() { | 206 void QuicSimpleServerStream::SendNotFoundResponse() { |
| 207 QUIC_DVLOG(1) << "Sending not found response for stream " << id(); | 207 QUIC_DVLOG(1) << "Stream " << id() << " sending not found response."; |
| 208 SpdyHeaderBlock headers; | 208 SpdyHeaderBlock headers; |
| 209 headers[":status"] = "404"; | 209 headers[":status"] = "404"; |
| 210 headers["content-length"] = | 210 headers["content-length"] = |
| 211 QuicTextUtils::Uint64ToString(strlen(kNotFoundResponseBody)); | 211 QuicTextUtils::Uint64ToString(strlen(kNotFoundResponseBody)); |
| 212 SendHeadersAndBody(std::move(headers), kNotFoundResponseBody); | 212 SendHeadersAndBody(std::move(headers), kNotFoundResponseBody); |
| 213 } | 213 } |
| 214 | 214 |
| 215 void QuicSimpleServerStream::SendErrorResponse() { | 215 void QuicSimpleServerStream::SendErrorResponse() { |
| 216 QUIC_DVLOG(1) << "Sending error response for stream " << id(); | 216 QUIC_DVLOG(1) << "Stream " << id() << " sending error response."; |
| 217 SpdyHeaderBlock headers; | 217 SpdyHeaderBlock headers; |
| 218 headers[":status"] = "500"; | 218 headers[":status"] = "500"; |
| 219 headers["content-length"] = | 219 headers["content-length"] = |
| 220 QuicTextUtils::Uint64ToString(strlen(kErrorResponseBody)); | 220 QuicTextUtils::Uint64ToString(strlen(kErrorResponseBody)); |
| 221 SendHeadersAndBody(std::move(headers), kErrorResponseBody); | 221 SendHeadersAndBody(std::move(headers), kErrorResponseBody); |
| 222 } | 222 } |
| 223 | 223 |
| 224 void QuicSimpleServerStream::SendHeadersAndBody( | 224 void QuicSimpleServerStream::SendHeadersAndBody( |
| 225 SpdyHeaderBlock response_headers, | 225 SpdyHeaderBlock response_headers, |
| 226 StringPiece body) { | 226 StringPiece body) { |
| 227 SendHeadersAndBodyAndTrailers(std::move(response_headers), body, | 227 SendHeadersAndBodyAndTrailers(std::move(response_headers), body, |
| 228 SpdyHeaderBlock()); | 228 SpdyHeaderBlock()); |
| 229 } | 229 } |
| 230 | 230 |
| 231 void QuicSimpleServerStream::SendHeadersAndBodyAndTrailers( | 231 void QuicSimpleServerStream::SendHeadersAndBodyAndTrailers( |
| 232 SpdyHeaderBlock response_headers, | 232 SpdyHeaderBlock response_headers, |
| 233 StringPiece body, | 233 StringPiece body, |
| 234 SpdyHeaderBlock response_trailers) { | 234 SpdyHeaderBlock response_trailers) { |
| 235 if (!allow_bidirectional_data() && !reading_stopped()) { | 235 if (!allow_bidirectional_data() && !reading_stopped()) { |
| 236 StopReading(); | 236 StopReading(); |
| 237 } | 237 } |
| 238 | 238 |
| 239 // Send the headers, with a FIN if there's nothing else to send. | 239 // Send the headers, with a FIN if there's nothing else to send. |
| 240 bool send_fin = (body.empty() && response_trailers.empty()); | 240 bool send_fin = (body.empty() && response_trailers.empty()); |
| 241 QUIC_DLOG(INFO) << "Writing headers (fin = " << send_fin | 241 QUIC_DLOG(INFO) << "Stream " << id() << " writing headers (fin = " << send_fin |
| 242 << ") : " << response_headers.DebugString(); | 242 << ") : " << response_headers.DebugString(); |
| 243 WriteHeaders(std::move(response_headers), send_fin, nullptr); | 243 WriteHeaders(std::move(response_headers), send_fin, nullptr); |
| 244 if (send_fin) { | 244 if (send_fin) { |
| 245 // Nothing else to send. | 245 // Nothing else to send. |
| 246 return; | 246 return; |
| 247 } | 247 } |
| 248 | 248 |
| 249 // Send the body, with a FIN if there's no trailers to send. | 249 // Send the body, with a FIN if there's no trailers to send. |
| 250 send_fin = response_trailers.empty(); | 250 send_fin = response_trailers.empty(); |
| 251 QUIC_DLOG(INFO) << "Writing body (fin = " << send_fin | 251 QUIC_DLOG(INFO) << "Stream " << id() << " writing body (fin = " << send_fin |
| 252 << ") with size: " << body.size(); | 252 << ") with size: " << body.size(); |
| 253 if (!body.empty() || send_fin) { | 253 if (!body.empty() || send_fin) { |
| 254 WriteOrBufferData(body, send_fin, nullptr); | 254 WriteOrBufferData(body, send_fin, nullptr); |
| 255 } | 255 } |
| 256 if (send_fin) { | 256 if (send_fin) { |
| 257 // Nothing else to send. | 257 // Nothing else to send. |
| 258 return; | 258 return; |
| 259 } | 259 } |
| 260 | 260 |
| 261 // Send the trailers. A FIN is always sent with trailers. | 261 // Send the trailers. A FIN is always sent with trailers. |
| 262 QUIC_DLOG(INFO) << "Writing trailers (fin = true): " | 262 QUIC_DLOG(INFO) << "Stream " << id() << " writing trailers (fin = true): " |
| 263 << response_trailers.DebugString(); | 263 << response_trailers.DebugString(); |
| 264 WriteTrailers(std::move(response_trailers), nullptr); | 264 WriteTrailers(std::move(response_trailers), nullptr); |
| 265 } | 265 } |
| 266 | 266 |
| 267 const char* const QuicSimpleServerStream::kErrorResponseBody = "bad"; | 267 const char* const QuicSimpleServerStream::kErrorResponseBody = "bad"; |
| 268 const char* const QuicSimpleServerStream::kNotFoundResponseBody = | 268 const char* const QuicSimpleServerStream::kNotFoundResponseBody = |
| 269 "file not found"; | 269 "file not found"; |
| 270 | 270 |
| 271 } // namespace net | 271 } // namespace net |
| OLD | NEW |