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
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stdio.h>
#include "mojo/public/cpp/application/application_delegate.h"
#include "mojo/public/cpp/application/application_impl.h"
#include "mojo/services/public/interfaces/network/network_service.mojom.h"
#include "mojo/services/public/interfaces/network/url_loader.mojom.h"
namespace mojo {
namespace examples {
class WGetApp : public ApplicationDelegate, public URLLoaderClient {
public:
virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE {
app->ConnectToService("mojo:mojo_network_service", &network_service_);
Start();
}
private:
virtual void OnReceivedRedirect(URLResponsePtr response,
const String& new_url,
const String& new_method) MOJO_OVERRIDE {
PrintResponse(response);
}
virtual void OnReceivedResponse(URLResponsePtr response) MOJO_OVERRIDE {
PrintResponse(response);
PrintResponseBody();
Start();
}
virtual void OnReceivedError(NetworkErrorPtr error) MOJO_OVERRIDE {
printf("Got error: %d (%s)\n",
error->code, error->description.get().c_str());
}
virtual void OnReceivedEndOfResponseBody() MOJO_OVERRIDE {
// Ignored.
}
void Start() {
std::string url = PromptForURL();
printf("Loading: %s\n", url.c_str());
network_service_->CreateURLLoader(Get(&url_loader_));
url_loader_.set_client(this);
URLRequestPtr request(URLRequest::New());
request->url = url;
request->method = "GET";
request->auto_follow_redirects = true;
DataPipe data_pipe;
response_body_stream_ = data_pipe.consumer_handle.Pass();
url_loader_->Start(request.Pass(), data_pipe.producer_handle.Pass());
}
std::string PromptForURL() {
printf("Enter URL> ");
char buf[1024];
if (scanf("%1023s", buf) != 1)
buf[0] = '\0';
return buf;
}
void PrintResponse(const URLResponsePtr& response) {
printf(">>> Headers <<< \n");
printf(" %s\n", response->status_line.get().c_str());
if (response->headers) {
for (size_t i = 0; i < response->headers.size(); ++i)
printf(" %s\n", response->headers[i].get().c_str());
}
}
void PrintResponseBody() {
// Read response body in blocking fashion.
printf(">>> Body <<<\n");
for (;;) {
char buf[512];
uint32_t num_bytes = sizeof(buf);
MojoResult result = ReadDataRaw(
response_body_stream_.get(),
buf,
&num_bytes,
MOJO_READ_DATA_FLAG_NONE);
if (result == MOJO_RESULT_SHOULD_WAIT) {
Wait(response_body_stream_.get(),
MOJO_HANDLE_SIGNAL_READABLE,
MOJO_DEADLINE_INDEFINITE);
} else if (result == MOJO_RESULT_OK) {
if (fwrite(buf, num_bytes, 1, stdout) != num_bytes) {
printf("\nUnexpected error writing to file\n");
break;
}
} else {
break;
}
}
printf("\n>>> EOF <<<\n");
}
NetworkServicePtr network_service_;
URLLoaderPtr url_loader_;
ScopedDataPipeConsumerHandle response_body_stream_;
};
} // namespace examples
// static
ApplicationDelegate* ApplicationDelegate::Create() {
return new examples::WGetApp();
}
} // namespace mojo
|