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: |
Aaron Boodman
2014/07/10 20:05:00
Out of curiosity, why have a separate ResponsePrin
| |
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 fwrite(buf, num_bytes, 1, stdout); | 54 fwrite(buf, num_bytes, 1, stdout); |
97 } else { | 55 } else { |
98 break; | 56 break; |
99 } | 57 } |
100 } | 58 } |
101 | 59 |
102 printf("\n>>> EOF <<<\n"); | 60 printf("\n>>> EOF <<<\n"); |
103 } | 61 } |
62 }; | |
63 | |
64 } // namespace | |
65 | |
66 class WGetApp : public ApplicationDelegate { | |
67 public: | |
68 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE { | |
69 app->ConnectToService("mojo:mojo_network_service", &network_service_); | |
70 Start(); | |
71 } | |
72 | |
73 private: | |
74 void Start() { | |
75 std::string url = PromptForURL(); | |
76 printf("Loading: %s\n", url.c_str()); | |
77 | |
78 network_service_->CreateURLLoader(Get(&url_loader_)); | |
79 | |
80 URLRequestPtr request(URLRequest::New()); | |
81 request->url = url; | |
82 request->method = "GET"; | |
83 request->auto_follow_redirects = true; | |
84 | |
85 url_loader_->Start(request.Pass(), ResponsePrinter()); | |
86 } | |
87 | |
88 std::string PromptForURL() { | |
89 printf("Enter URL> "); | |
90 char buf[1024]; | |
91 if (scanf("%1023s", buf) != 1) | |
92 buf[0] = '\0'; | |
93 return buf; | |
94 } | |
104 | 95 |
105 NetworkServicePtr network_service_; | 96 NetworkServicePtr network_service_; |
106 URLLoaderPtr url_loader_; | 97 URLLoaderPtr url_loader_; |
107 ScopedDataPipeConsumerHandle response_body_stream_; | |
108 }; | 98 }; |
109 | 99 |
110 } // namespace examples | 100 } // namespace examples |
111 | 101 |
112 // static | 102 // static |
113 ApplicationDelegate* ApplicationDelegate::Create() { | 103 ApplicationDelegate* ApplicationDelegate::Create() { |
114 return new examples::WGetApp(); | 104 return new examples::WGetApp(); |
115 } | 105 } |
116 | 106 |
117 } // namespace mojo | 107 } // namespace mojo |
OLD | NEW |