| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/http/http_network_transaction.h" | 5 #include "net/http/http_network_transaction.h" |
| 6 | 6 |
| 7 #include <math.h> // ceil | 7 #include <math.h> // ceil |
| 8 #include <stdarg.h> | 8 #include <stdarg.h> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 7706 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7717 kuint64max, | 7717 kuint64max, |
| 7718 base::Time())); | 7718 base::Time())); |
| 7719 UploadDataStream upload_data_stream(element_readers.Pass(), 0); | 7719 UploadDataStream upload_data_stream(element_readers.Pass(), 0); |
| 7720 | 7720 |
| 7721 HttpRequestInfo request; | 7721 HttpRequestInfo request; |
| 7722 request.method = "POST"; | 7722 request.method = "POST"; |
| 7723 request.url = GURL("http://www.google.com/upload"); | 7723 request.url = GURL("http://www.google.com/upload"); |
| 7724 request.upload_data_stream = &upload_data_stream; | 7724 request.upload_data_stream = &upload_data_stream; |
| 7725 request.load_flags = 0; | 7725 request.load_flags = 0; |
| 7726 | 7726 |
| 7727 // If we try to upload an unreadable file, the network stack should report | 7727 // If we try to upload an unreadable file, the transaction should fail. |
| 7728 // the file size as zero and upload zero bytes for that file. | |
| 7729 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | 7728 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 7730 scoped_ptr<HttpTransaction> trans( | 7729 scoped_ptr<HttpTransaction> trans( |
| 7731 new HttpNetworkTransaction(DEFAULT_PRIORITY, session)); | 7730 new HttpNetworkTransaction(DEFAULT_PRIORITY, session)); |
| 7732 | 7731 |
| 7733 MockRead data_reads[] = { | 7732 StaticSocketDataProvider data(NULL, 0, NULL, 0); |
| 7734 MockRead("HTTP/1.0 200 OK\r\n\r\n"), | |
| 7735 MockRead(SYNCHRONOUS, OK), | |
| 7736 }; | |
| 7737 MockWrite data_writes[] = { | |
| 7738 MockWrite("POST /upload HTTP/1.1\r\n" | |
| 7739 "Host: www.google.com\r\n" | |
| 7740 "Connection: keep-alive\r\n" | |
| 7741 "Content-Length: 0\r\n\r\n"), | |
| 7742 MockWrite(SYNCHRONOUS, OK), | |
| 7743 }; | |
| 7744 StaticSocketDataProvider data(data_reads, arraysize(data_reads), data_writes, | |
| 7745 arraysize(data_writes)); | |
| 7746 session_deps_.socket_factory->AddSocketDataProvider(&data); | 7733 session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 7747 | 7734 |
| 7748 TestCompletionCallback callback; | 7735 TestCompletionCallback callback; |
| 7749 | 7736 |
| 7750 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | 7737 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
| 7751 EXPECT_EQ(ERR_IO_PENDING, rv); | 7738 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7752 | 7739 |
| 7753 rv = callback.WaitForResult(); | 7740 rv = callback.WaitForResult(); |
| 7754 EXPECT_EQ(OK, rv); | 7741 EXPECT_EQ(ERR_ACCESS_DENIED, rv); |
| 7755 | 7742 |
| 7756 const HttpResponseInfo* response = trans->GetResponseInfo(); | 7743 const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 7757 ASSERT_TRUE(response != NULL); | 7744 EXPECT_FALSE(response); |
| 7758 EXPECT_TRUE(response->headers.get() != NULL); | |
| 7759 EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); | |
| 7760 | 7745 |
| 7761 base::DeleteFile(temp_file, false); | 7746 base::DeleteFile(temp_file, false); |
| 7762 } | 7747 } |
| 7763 | |
| 7764 TEST_P(HttpNetworkTransactionTest, UnreadableUploadFileAfterAuthRestart) { | |
| 7765 base::FilePath temp_file; | |
| 7766 ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file)); | |
| 7767 std::string temp_file_contents("Unreadable file."); | |
| 7768 std::string unreadable_contents(temp_file_contents.length(), '\0'); | |
| 7769 ASSERT_TRUE(file_util::WriteFile(temp_file, temp_file_contents.c_str(), | |
| 7770 temp_file_contents.length())); | |
| 7771 | |
| 7772 ScopedVector<UploadElementReader> element_readers; | |
| 7773 element_readers.push_back( | |
| 7774 new UploadFileElementReader(base::MessageLoopProxy::current().get(), | |
| 7775 temp_file, | |
| 7776 0, | |
| 7777 kuint64max, | |
| 7778 base::Time())); | |
| 7779 UploadDataStream upload_data_stream(element_readers.Pass(), 0); | |
| 7780 | |
| 7781 HttpRequestInfo request; | |
| 7782 request.method = "POST"; | |
| 7783 request.url = GURL("http://www.google.com/upload"); | |
| 7784 request.upload_data_stream = &upload_data_stream; | |
| 7785 request.load_flags = 0; | |
| 7786 | |
| 7787 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
| 7788 scoped_ptr<HttpTransaction> trans( | |
| 7789 new HttpNetworkTransaction(DEFAULT_PRIORITY, session)); | |
| 7790 | |
| 7791 MockRead data_reads[] = { | |
| 7792 MockRead("HTTP/1.1 401 Unauthorized\r\n"), | |
| 7793 MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), | |
| 7794 MockRead("Content-Length: 0\r\n\r\n"), // No response body. | |
| 7795 | |
| 7796 MockRead("HTTP/1.1 200 OK\r\n"), | |
| 7797 MockRead("Content-Length: 0\r\n\r\n"), | |
| 7798 MockRead(SYNCHRONOUS, OK), | |
| 7799 }; | |
| 7800 MockWrite data_writes[] = { | |
| 7801 MockWrite("POST /upload HTTP/1.1\r\n" | |
| 7802 "Host: www.google.com\r\n" | |
| 7803 "Connection: keep-alive\r\n" | |
| 7804 "Content-Length: 16\r\n\r\n"), | |
| 7805 MockWrite(SYNCHRONOUS, temp_file_contents.c_str()), | |
| 7806 | |
| 7807 MockWrite("POST /upload HTTP/1.1\r\n" | |
| 7808 "Host: www.google.com\r\n" | |
| 7809 "Connection: keep-alive\r\n" | |
| 7810 "Content-Length: 0\r\n" | |
| 7811 "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), | |
| 7812 MockWrite(SYNCHRONOUS, unreadable_contents.c_str(), | |
| 7813 temp_file_contents.length()), | |
| 7814 MockWrite(SYNCHRONOUS, OK), | |
| 7815 }; | |
| 7816 StaticSocketDataProvider data(data_reads, arraysize(data_reads), data_writes, | |
| 7817 arraysize(data_writes)); | |
| 7818 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
| 7819 | |
| 7820 TestCompletionCallback callback1; | |
| 7821 | |
| 7822 int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); | |
| 7823 EXPECT_EQ(ERR_IO_PENDING, rv); | |
| 7824 | |
| 7825 rv = callback1.WaitForResult(); | |
| 7826 EXPECT_EQ(OK, rv); | |
| 7827 | |
| 7828 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
| 7829 ASSERT_TRUE(response != NULL); | |
| 7830 ASSERT_TRUE(response->headers.get() != NULL); | |
| 7831 EXPECT_EQ("HTTP/1.1 401 Unauthorized", response->headers->GetStatusLine()); | |
| 7832 EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); | |
| 7833 | |
| 7834 // Now make the file unreadable and try again. | |
| 7835 ASSERT_TRUE(file_util::MakeFileUnreadable(temp_file)); | |
| 7836 | |
| 7837 TestCompletionCallback callback2; | |
| 7838 | |
| 7839 rv = trans->RestartWithAuth( | |
| 7840 AuthCredentials(kFoo, kBar), callback2.callback()); | |
| 7841 EXPECT_EQ(ERR_IO_PENDING, rv); | |
| 7842 | |
| 7843 rv = callback2.WaitForResult(); | |
| 7844 EXPECT_EQ(OK, rv); | |
| 7845 | |
| 7846 response = trans->GetResponseInfo(); | |
| 7847 ASSERT_TRUE(response != NULL); | |
| 7848 EXPECT_TRUE(response->headers.get() != NULL); | |
| 7849 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
| 7850 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
| 7851 | |
| 7852 base::DeleteFile(temp_file, false); | |
| 7853 } | |
| 7854 | 7748 |
| 7855 TEST_P(HttpNetworkTransactionTest, CancelDuringInitRequestBody) { | 7749 TEST_P(HttpNetworkTransactionTest, CancelDuringInitRequestBody) { |
| 7856 class FakeUploadElementReader : public UploadElementReader { | 7750 class FakeUploadElementReader : public UploadElementReader { |
| 7857 public: | 7751 public: |
| 7858 FakeUploadElementReader() {} | 7752 FakeUploadElementReader() {} |
| 7859 virtual ~FakeUploadElementReader() {} | 7753 virtual ~FakeUploadElementReader() {} |
| 7860 | 7754 |
| 7861 const CompletionCallback& callback() const { return callback_; } | 7755 const CompletionCallback& callback() const { return callback_; } |
| 7862 | 7756 |
| 7863 // UploadElementReader overrides: | 7757 // UploadElementReader overrides: |
| (...skipping 4438 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 12302 // established, to let the HTTP request start. | 12196 // established, to let the HTTP request start. |
| 12303 ASSERT_EQ(OK, http_callback.WaitForResult()); | 12197 ASSERT_EQ(OK, http_callback.WaitForResult()); |
| 12304 std::string response_data; | 12198 std::string response_data; |
| 12305 ASSERT_EQ(OK, ReadTransaction(http_trans.get(), &response_data)); | 12199 ASSERT_EQ(OK, ReadTransaction(http_trans.get(), &response_data)); |
| 12306 EXPECT_EQ("falafel", response_data); | 12200 EXPECT_EQ("falafel", response_data); |
| 12307 | 12201 |
| 12308 EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session)); | 12202 EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session)); |
| 12309 } | 12203 } |
| 12310 | 12204 |
| 12311 } // namespace net | 12205 } // namespace net |
| OLD | NEW |