forked from nikhilk/scriptsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebCommand.cs
More file actions
206 lines (173 loc) · 7.25 KB
/
Copy pathWebCommand.cs
File metadata and controls
206 lines (173 loc) · 7.25 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
// WebCommand.cs
//
using System;
using System.Collections.Generic;
using System.Serialization;
using NodeApi;
using NodeApi.IO;
using NodeApi.Network;
namespace WebREPL {
internal sealed class WebCommand {
private HttpVerb _verb;
private UrlData _urlData;
private WebRequest _webRequest;
private WebResponse _webResponse;
private AsyncResultCallback<object> _commandCallback;
private WebCommand(HttpVerb verb, UrlData urlData, WebRequest webRequest, WebResponse webResponse) {
_verb = verb;
_urlData = urlData;
_webRequest = webRequest;
_webResponse = webResponse;
}
private HttpClientOptions BuildHttpOptions() {
HttpClientOptions options = new HttpClientOptions();
options.HostName = _urlData.HostName;
if (String.IsNullOrEmpty(_urlData.Port) == false) {
options.Port = Int32.Parse(_urlData.Port);
}
Dictionary<string, string> query = new Dictionary<string, string>();
bool appendQuery = false;
if ((_webRequest.Query != null) || (_urlData.Query != null)) {
if (_webRequest.Query != null) {
foreach (KeyValuePair<string, string> param in _webRequest.Query) {
query[param.Key] = param.Value;
appendQuery = true;
}
}
if (_urlData.Query != null) {
foreach (KeyValuePair<string, string> param in _urlData.Query) {
query[param.Key] = param.Value;
appendQuery = true;
}
}
}
StringBuilder pathBuilder = new StringBuilder();
if (String.IsNullOrEmpty(_urlData.PathName) == false) {
pathBuilder.Append(_urlData.PathName);
}
else {
pathBuilder.Append("/");
}
if (appendQuery) {
pathBuilder.Append("?");
bool first = true;
foreach (KeyValuePair<string, string> param in query) {
if (first == false) {
pathBuilder.Append("&");
}
pathBuilder.Append(param.Key);
pathBuilder.Append("=");
pathBuilder.Append(param.Value.EncodeUriComponent());
}
}
options.Path = pathBuilder.ToString();
options.Headers = new Dictionary<string, string>();
options.Headers["Accept"] = "*/*";
if (_webRequest.Headers != null) {
foreach (KeyValuePair<string, string> header in _webRequest.Headers) {
options.Headers[header.Key] = header.Value;
}
}
options.Headers["Host"] = _urlData.HostName;
if ((String.IsNullOrEmpty(_webRequest.UserName) == false) &&
(String.IsNullOrEmpty(_webRequest.Password) == false)) {
string creds = _webRequest.UserName + ':' + _webRequest.Password;
options.Headers["Authorization"] = "Basic " + new Buffer(creds).ToString(Encoding.Base64);
}
return options;
}
public void HandleCommand(AsyncResultCallback<object> callback) {
_commandCallback = callback;
HttpClientOptions options = BuildHttpOptions();
string requestData = null;
if ((_verb == HttpVerb.POST) || (_verb == HttpVerb.PUT)) {
if (_webRequest.Data != null) {
string data;
if (_webRequest.Data.GetType() == typeof(string)) {
data = (string)_webRequest.Data;
if (options.Headers.ContainsKey("Content-Type") == false) {
options.Headers["Content-Type"] = "text/plain";
}
}
else {
data = Json.Stringify(_webRequest.Data);
options.Headers["Content-Type"] = "application/json";
}
Buffer requestBuffer = new Buffer(data);
requestData = requestBuffer.ToString(Encoding.UTF8);
options.Headers["Content-Length"] = requestData.Length.ToString();
}
}
HttpClientRequest request;
if (_urlData.Protocol == "http:") {
request = Http.Request(options, HandleResponse);
}
else {
request = Https.Request(options, HandleResponse);
}
if (requestData != null) {
request.Write(requestData);
}
request.End();
}
private void HandleResponse(HttpClientResponse response) {
string responseData = String.Empty;
response.SetEncoding(Encoding.UTF8);
response.Data += delegate(string chunk) {
responseData += chunk;
};
response.End += delegate() {
_webResponse.StatusCode = response.StatusCode;
_webResponse.Headers = response.Headers;
_webResponse.ResponseText = responseData;
if (response.Headers["Content-Type"] == "application/json") {
_webResponse.Response = Json.Parse(responseData);
}
else {
_webResponse.Response = responseData;
}
_commandCallback(null, _webResponse.Response);
};
}
public static WebCommand TryParseCommand(string command, WebRequest webRequest, WebResponse webResponse) {
HttpVerb verb = HttpVerb.GET;
string uri = null;
string altCommand = command.ToLowerCase();
if (altCommand.StartsWith("get ")) {
verb = HttpVerb.GET;
uri = command.Substring(4);
}
else if (altCommand.StartsWith("post ")) {
verb = HttpVerb.POST;
uri = command.Substring(5);
}
else if (altCommand.StartsWith("put ")) {
verb = HttpVerb.PUT;
uri = command.Substring(4);
}
else if (altCommand.StartsWith("delete ")) {
verb = HttpVerb.DELETE;
uri = command.Substring(7);
}
else if (altCommand.StartsWith("head ")) {
verb = HttpVerb.HEAD;
uri = command.Substring(5);
}
else if (altCommand.StartsWith("options ")) {
verb = HttpVerb.OPTIONS;
uri = command.Substring(8);
}
if (uri != null) {
uri = uri.Trim();
if (String.IsNullOrEmpty(uri) == false) {
UrlData urlData = Url.Parse(uri, /* parseQueryString */ true);
if ((urlData.Protocol == "http:") ||
(urlData.Protocol == "https:")) {
return new WebCommand(verb, urlData, webRequest, webResponse);
}
}
}
return null;
}
}
}