| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <stdio.h> | 5 #include <stdio.h> |
| 6 | 6 |
| 7 #include "mojo/public/cpp/application/application_delegate.h" | 7 #include "mojo/public/cpp/application/application_delegate.h" |
| 8 #include "mojo/public/cpp/application/application_impl.h" | 8 #include "mojo/public/cpp/application/application_impl.h" |
| 9 #include "mojo/public/cpp/utility/run_loop.h" |
| 9 #include "mojo/services/public/interfaces/network/network_service.mojom.h" | 10 #include "mojo/services/public/interfaces/network/network_service.mojom.h" |
| 10 #include "mojo/services/public/interfaces/network/url_loader.mojom.h" | 11 #include "mojo/services/public/interfaces/network/url_loader.mojom.h" |
| 11 | 12 |
| 12 namespace mojo { | 13 namespace mojo { |
| 13 namespace examples { | 14 namespace examples { |
| 15 namespace { |
| 14 | 16 |
| 15 class WGetApp : public ApplicationDelegate, public URLLoaderClient { | 17 class ResponsePrinter { |
| 16 public: | 18 public: |
| 17 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE { | 19 void Run(URLResponsePtr response) const { |
| 18 app->ConnectToService("mojo:mojo_network_service", &network_service_); | 20 if (response->error) { |
| 19 Start(); | 21 printf("Got error: %d (%s)\n", |
| 22 response->error->code, response->error->description.get().c_str()); |
| 23 } else { |
| 24 PrintResponse(response); |
| 25 PrintResponseBody(response->body.Pass()); |
| 26 } |
| 27 |
| 28 RunLoop::current()->Quit(); // All done! |
| 20 } | 29 } |
| 21 | 30 |
| 22 private: | 31 void PrintResponse(const URLResponsePtr& response) const { |
| 23 virtual void OnReceivedRedirect(URLResponsePtr response, | |
| 24 const String& new_url, | |
| 25 const String& new_method) MOJO_OVERRIDE { | |
| 26 PrintResponse(response); | |
| 27 } | |
| 28 | |
| 29 virtual void OnReceivedResponse(URLResponsePtr response) MOJO_OVERRIDE { | |
| 30 PrintResponse(response); | |
| 31 PrintResponseBody(); | |
| 32 Start(); | |
| 33 } | |
| 34 | |
| 35 virtual void OnReceivedError(NetworkErrorPtr error) MOJO_OVERRIDE { | |
| 36 printf("Got error: %d (%s)\n", | |
| 37 error->code, error->description.get().c_str()); | |
| 38 } | |
| 39 | |
| 40 virtual void OnReceivedEndOfResponseBody() MOJO_OVERRIDE { | |
| 41 // Ignored. | |
| 42 } | |
| 43 | |
| 44 void Start() { | |
| 45 std::string url = PromptForURL(); | |
| 46 printf("Loading: %s\n", url.c_str()); | |
| 47 | |
| 48 network_service_->CreateURLLoader(Get(&url_loader_)); | |
| 49 url_loader_.set_client(this); | |
| 50 | |
| 51 URLRequestPtr request(URLRequest::New()); | |
| 52 request->url = url; | |
| 53 request->method = "GET"; | |
| 54 request->auto_follow_redirects = true; | |
| 55 | |
| 56 DataPipe data_pipe; | |
| 57 response_body_stream_ = data_pipe.consumer_handle.Pass(); | |
| 58 | |
| 59 url_loader_->Start(request.Pass(), data_pipe.producer_handle.Pass()); | |
| 60 } | |
| 61 | |
| 62 std::string PromptForURL() { | |
| 63 printf("Enter URL> "); | |
| 64 char buf[1024]; | |
| 65 if (scanf("%1023s", buf) != 1) | |
| 66 buf[0] = '\0'; | |
| 67 return buf; | |
| 68 } | |
| 69 | |
| 70 void PrintResponse(const URLResponsePtr& response) { | |
| 71 printf(">>> Headers <<< \n"); | 32 printf(">>> Headers <<< \n"); |
| 72 printf(" %s\n", response->status_line.get().c_str()); | 33 printf(" %s\n", response->status_line.get().c_str()); |
| 73 if (response->headers) { | 34 if (response->headers) { |
| 74 for (size_t i = 0; i < response->headers.size(); ++i) | 35 for (size_t i = 0; i < response->headers.size(); ++i) |
| 75 printf(" %s\n", response->headers[i].get().c_str()); | 36 printf(" %s\n", response->headers[i].get().c_str()); |
| 76 } | 37 } |
| 77 } | 38 } |
| 78 | 39 |
| 79 void PrintResponseBody() { | 40 void PrintResponseBody(ScopedDataPipeConsumerHandle body) const { |
| 80 // Read response body in blocking fashion. | 41 // Read response body in blocking fashion. |
| 81 printf(">>> Body <<<\n"); | 42 printf(">>> Body <<<\n"); |
| 82 | 43 |
| 83 for (;;) { | 44 for (;;) { |
| 84 char buf[512]; | 45 char buf[512]; |
| 85 uint32_t num_bytes = sizeof(buf); | 46 uint32_t num_bytes = sizeof(buf); |
| 86 MojoResult result = ReadDataRaw( | 47 MojoResult result = ReadDataRaw(body.get(), buf, &num_bytes, |
| 87 response_body_stream_.get(), | 48 MOJO_READ_DATA_FLAG_NONE); |
| 88 buf, | |
| 89 &num_bytes, | |
| 90 MOJO_READ_DATA_FLAG_NONE); | |
| 91 if (result == MOJO_RESULT_SHOULD_WAIT) { | 49 if (result == MOJO_RESULT_SHOULD_WAIT) { |
| 92 Wait(response_body_stream_.get(), | 50 Wait(body.get(), |
| 93 MOJO_HANDLE_SIGNAL_READABLE, | 51 MOJO_HANDLE_SIGNAL_READABLE, |
| 94 MOJO_DEADLINE_INDEFINITE); | 52 MOJO_DEADLINE_INDEFINITE); |
| 95 } else if (result == MOJO_RESULT_OK) { | 53 } else if (result == MOJO_RESULT_OK) { |
| 96 if (fwrite(buf, num_bytes, 1, stdout) != num_bytes) { | 54 if (fwrite(buf, num_bytes, 1, stdout) != num_bytes) { |
| 97 printf("\nUnexpected error writing to file\n"); | 55 printf("\nUnexpected error writing to file\n"); |
| 98 break; | 56 break; |
| 99 } | 57 } |
| 100 } else { | 58 } else { |
| 101 break; | 59 break; |
| 102 } | 60 } |
| 103 } | 61 } |
| 104 | 62 |
| 105 printf("\n>>> EOF <<<\n"); | 63 printf("\n>>> EOF <<<\n"); |
| 106 } | 64 } |
| 65 }; |
| 66 |
| 67 } // namespace |
| 68 |
| 69 class WGetApp : public ApplicationDelegate { |
| 70 public: |
| 71 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE { |
| 72 app->ConnectToService("mojo:mojo_network_service", &network_service_); |
| 73 Start(); |
| 74 } |
| 75 |
| 76 private: |
| 77 void Start() { |
| 78 std::string url = PromptForURL(); |
| 79 printf("Loading: %s\n", url.c_str()); |
| 80 |
| 81 network_service_->CreateURLLoader(Get(&url_loader_)); |
| 82 |
| 83 URLRequestPtr request(URLRequest::New()); |
| 84 request->url = url; |
| 85 request->method = "GET"; |
| 86 request->auto_follow_redirects = true; |
| 87 |
| 88 url_loader_->Start(request.Pass(), ResponsePrinter()); |
| 89 } |
| 90 |
| 91 std::string PromptForURL() { |
| 92 printf("Enter URL> "); |
| 93 char buf[1024]; |
| 94 if (scanf("%1023s", buf) != 1) |
| 95 buf[0] = '\0'; |
| 96 return buf; |
| 97 } |
| 107 | 98 |
| 108 NetworkServicePtr network_service_; | 99 NetworkServicePtr network_service_; |
| 109 URLLoaderPtr url_loader_; | 100 URLLoaderPtr url_loader_; |
| 110 ScopedDataPipeConsumerHandle response_body_stream_; | |
| 111 }; | 101 }; |
| 112 | 102 |
| 113 } // namespace examples | 103 } // namespace examples |
| 114 | 104 |
| 115 // static | 105 // static |
| 116 ApplicationDelegate* ApplicationDelegate::Create() { | 106 ApplicationDelegate* ApplicationDelegate::Create() { |
| 117 return new examples::WGetApp(); | 107 return new examples::WGetApp(); |
| 118 } | 108 } |
| 119 | 109 |
| 120 } // namespace mojo | 110 } // namespace mojo |
| OLD | NEW |