-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathxll_inet.cpp
More file actions
223 lines (197 loc) · 6.42 KB
/
xll_inet.cpp
File metadata and controls
223 lines (197 loc) · 6.42 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// xllinet.cpp - WinInet wrappers
#include "inet.h"
#include "xll/xll.h"
#ifndef CATEGORY
#define CATEGORY L"INET"
#endif
#define INET_AGENT L"xllinet"
using xcstr = const WCHAR*;
using namespace Win;
using namespace xll;
AddIn xai_inet_open(
Function(XLL_HANDLE, L"?xll_inet_open", L"INET.OPEN")
.Arg(XLL_CSTRING, L"agent", L"is the http user agent.")
.Uncalced()
.Category(CATEGORY)
.FunctionHelp(L"Return an internet connection handle.")
);
HANDLEX WINAPI xll_inet_open(xcstr agent)
{
#pragma XLLEXPORT
handlex h;
try {
if (!*agent)
agent = INET_AGENT;
handle<Internet::Open> h_(new Internet::Open(agent));
h = h_.get();
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
return h;
}
AddIn xai_inet_open_url(
Function(XLL_HANDLE, L"?xll_inet_open_url", L"INET.OPEN.URL")
.Arg(XLL_HANDLE, L"open", L"is a handle returned by INET.OPEN.")
.Arg(XLL_CSTRING, L"url", L"is the universal resource locator to open.")
.Arg(XLL_CSTRING, L"headers", L"are option headers to send to the HTTP server.")
.Uncalced()
.Category(CATEGORY)
.FunctionHelp(L"Return an internet open_urlion handle.")
);
HANDLEX WINAPI xll_inet_open_url(HANDLEX open, xcstr url, xcstr headers)
{
#pragma XLLEXPORT
handlex h;
try {
handle<Internet::Open> open_(open);
ensure(open_);
handle<Internet::OpenUrl> url_(new Internet::OpenUrl(*open_, url, headers));
h = url_.get();
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
return h;
}
AddIn xai_inet_read_file(
Function(XLL_LPOPER, L"?xll_inet_read_file", L"INET.READ.FILE")
.Arg(XLL_HANDLE, L"url", L"is a handle returned by INET.OPEN.URL.")
.Category(CATEGORY)
.FunctionHelp(L"Return the data from url.")
);
LPOPER WINAPI xll_inet_read_file(HANDLEX url)
{
#pragma XLLEXPORT
static OPER result;
try {
handle<Internet::OpenUrl> url_(url);
ensure(url_);
auto query = url_->ReadFile();
if (query.has_value())
result = query.value().c_str();
else
result = OPER(xlerr::NA);
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
return &result;
}
AddIn xai_inet_connect(
Function(XLL_HANDLE, L"?xll_inet_connect", L"INET.CONNECT")
.Arg(XLL_HANDLE, L"open", L"is a handle returned by INET.OPEN.")
.Arg(XLL_CSTRING, L"server", L"is the name of the server to connect to.")
.Arg(XLL_WORD, L"port", L"is the server port. Default is 80.")
.Uncalced()
.Category(CATEGORY)
.FunctionHelp(L"Return an internet connection handle.")
);
HANDLEX WINAPI xll_inet_connect(HANDLEX open, xcstr server, WORD port)
{
#pragma XLLEXPORT
handlex h;
try {
handle<Internet::Open> open_(open);
ensure(open_);
handle<Internet::Connect> conn_(new Internet::Connect(*open_, server, port));
h = conn_.get();
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
return h;
}
AddIn xai_inet_http_open_request(
Function(XLL_HANDLE, L"?xll_inet_http_open_request", L"INET.HTTP.OPEN.REQUEST")
.Arg(XLL_HANDLE, L"connect", L"is a handle returned by INET.CONNECT.")
.Arg(XLL_CSTRING, L"url", L"is the url for an object.")
.Arg(XLL_CSTRING, L"verb", L"is the http opertion to perform. Default is GET.")
.Uncalced()
.Category(CATEGORY)
.FunctionHelp(L"Return an internet http_open_requestion handle.")
);
HANDLEX WINAPI xll_inet_http_open_request(HANDLEX conn, xcstr url, xcstr verb)
{
#pragma XLLEXPORT
handlex h;
try {
if (!*verb)
verb = L"GET";
handle<Internet::Connect> conn_(conn);
ensure(conn_);
handle<Internet::HttpOpenRequest> req_(new Internet::HttpOpenRequest(*conn_, verb, url));
h = req_.get();
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
return h;
}
AddIn xai_inet_http_send_request(
Function(XLL_HANDLE, L"?xll_inet_http_send_request", L"INET.HTTP.SEND.REQUEST")
.Arg(XLL_HANDLE, L"request", L"is a handle returned by INET.HTTP.OPEN.REQUEST.")
.Arg(XLL_PSTRING, L"headers", L"optional headers to send.")
.Arg(XLL_PSTRING, L"optional", L"optional additional data for PUT or POST operation.")
.Category(CATEGORY)
.FunctionHelp(L"Return an internet http_send_requestion handle.")
);
HANDLEX WINAPI xll_inet_http_send_request(HANDLEX request, xcstr headers, xcstr optional)
{
#pragma XLLEXPORT
try {
handle<Internet::HttpOpenRequest> request_(request);
ensure(request_);
ensure (request_->HttpSendRequest(headers+1, headers[0], (LPVOID)(optional + 1), optional[0]));
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
return request;
}
AddIn xai_inet_http_query_info(
Function(XLL_LPOPER, L"?xll_inet_http_query_info", L"INET.HTTP.QUERY.INFO")
.Arg(XLL_HANDLE, L"request", L"is a handle returned by INET.HTTP.OPEN.REQUEST.")
.Arg(XLL_WORD, L"info", L"is the information level to query using a HTTP_QUERY_* enumeration.")
.Category(CATEGORY)
.FunctionHelp(L"Return an internet http_query_infoion handle.")
);
LPOPER WINAPI xll_inet_http_query_info(HANDLEX request, WORD info)
{
#pragma XLLEXPORT
static OPER result;
try {
handle<Internet::HttpOpenRequest> request_(request);
auto query = request_->HttpQueryInfo(info);
if (query.has_value())
result = query.value().c_str();
else
result = OPER(xlerr::NA);
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
return &result;
}
#ifdef _DEBUG
test test_xll_inet([] {
{
Internet::Open open(INET_AGENT);
Internet::Connect conn(open, L"google.com", INTERNET_DEFAULT_HTTPS_PORT);
Internet::HttpOpenRequest req(conn, L"/index.html", L"GET");
req.HttpSendRequest(L"", 0, 0, 0);
auto q = req.HttpQueryInfo(HTTP_QUERY_RAW_HEADERS);
ensure (q.has_value());
// q.value() is padded with 0's at the end
ensure (0 == wcscmp(q.value().c_str(), L"HTTP/1.0 200 OK"));
}
{/*
Internet::Open open(INET_AGENT);
//HINTERNET hurl;
//hurl = ::InternetOpenUrl(open, L"https://google.com/index.html", NULL, 0, 0, 0);
Internet::OpenUrl url(open, L"https://google.com/index.html");
auto read = url.ReadFile();
ensure (read.has_value());
*/}
});
#endif // _DEBUG