This repository has been archived by the owner on Nov 21, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
urlopen.hppy
182 lines (135 loc) · 4.27 KB
/
urlopen.hppy
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Copyright (c) 2011, 2012 Timo Savola
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*/
#ifndef CONCRETE_MODULES_TAP_URLOPEN_HPP
#define CONCRETE_MODULES_TAP_URLOPEN_HPP
#include <concrete/continuation.hpp>
#include <concrete/io/buffer.hpp>
#include <concrete/io/url.hpp>
#include <concrete/objects/bytes.hpp>
#include <concrete/objects/dict.hpp>
#include <concrete/objects/string.hpp>
#include <concrete/objects/tuple.hpp>
#include <concrete/portable.hpp>
#include <concrete/resource.hpp>
#include <concrete/util/assert.hpp>
#include <concrete/util/noncopyable.hpp>
#include <concrete/util/trace.hpp>
namespace concrete {
class AbstractURLOpen: public Continuation {
{{{ DeclarePointer("AbstractURLOpen", "Continuation", has_construct=False, has_destroy=False) }}}
protected:
enum State { Open, Opening, ReceivingContent, Done };
enum { ResponseBufferSize = 16 * 1024 * 1024 };
struct Data: Noncopyable {
~Data() throw ()
{
reset();
}
void reset() throw ()
{
response.destroy();
opener.destroy();
}
bool is_lost() const throw ()
{
return response.is_lost() || opener.is_lost();
}
Portable<Object> url;
Portable<uint8_t> state;
PortableResource<Buffer> response;
PortableResource<URLOpener> opener;
} CONCRETE_PACKED;
public:
virtual ~AbstractURLOpen() {}
bool initiate(Object &result, const TupleObject &args, const DictObject &kwargs) const
{
data()->url = args.get_item(0).require<StringObject>();
// postpone actual work to resume() so that everything is set
// up by Execution::initiate_call
return leave_state(Open);
}
bool resume(Object &result) const
{
if (data()->is_lost()) {
Trace("urlopen resources lost");
data()->reset();
return leave_state(open(result));
}
State state = State(*data()->state);
switch (state) {
case Open: state = open(result); break;
case Opening: state = opening(result); break;
case ReceivingContent: state = receiving_content(result); break;
case Done: assert(false);
default: throw IntegrityError(address());
}
return leave_state(state);
}
protected:
bool leave_state(State state) const
{
data()->state = state;
return state == Done;
}
virtual State open(Object &result) const = 0;
State opening(Object &result) const
{
Trace("urlopen opening");
if (data()->opener->headers_received())
return receive_content(result);
data()->opener->suspend_until_headers_received();
return Opening;
}
State receive_content(Object &result) const
{
Trace("urlopen receive content");
auto length = data()->opener->response_length();
Trace("urlopen content length: %d", length);
if (length > ResponseBufferSize)
throw RuntimeError("HTTP response too long");
return receiving_content(result);
}
State receiving_content(Object &result) const
{
Trace("urlopen receiving content");
if (data()->opener->content_received())
return done(result);
data()->opener->suspend_until_content_received();
return ReceivingContent;
}
virtual State done(Object &result) const = 0;
};
class TapModule_URLOpen: public AbstractURLOpen {
{{{ DeclarePointer("TapModule_URLOpen", "AbstractURLOpen", has_construct=False, has_destroy=False) }}}
protected:
struct Data: AbstractURLOpen::Data {} CONCRETE_PACKED;
virtual State open(Object &result) const
{
Trace("urlopen open");
auto url = data()->url->require<StringObject>();
auto response = PortableResource<Buffer>::New(size_t(ResponseBufferSize));
data()->response = response;
auto opener = PortableResource<URLOpener>::New(url, *response);
data()->opener = opener;
return opening(result);
}
virtual State done(Object &result) const
{
Trace("urlopen done");
auto response = *data()->response;
auto size = response->consumable_size();
auto length = data()->opener->response_length();
if (length >= 0 && size_t(length) < size)
size = size_t(length);
result = BytesObject::New(reinterpret_cast<const uint8_t *> (response->consumable_data()), size);
return Done;
}
};
} // namespace
#endif