move general http business to http and out of the probe cli
This commit is contained in:
71
http.cm
71
http.cm
@@ -152,13 +152,82 @@ function sse_close(conn) {
|
|||||||
socket.close(conn)
|
socket.close(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function request(method, url, headers, body) {
|
||||||
|
var parts = array(url, "/")
|
||||||
|
var host_port = parts[2]
|
||||||
|
var path = "/" + text(array(parts, 3, length(parts)), "/")
|
||||||
|
var hp = array(host_port, ":")
|
||||||
|
var host = hp[0]
|
||||||
|
var port = length(hp) > 1 ? number(hp[1]) : 80
|
||||||
|
|
||||||
|
var fd = socket.socket("AF_INET", "SOCK_STREAM")
|
||||||
|
var raw = null
|
||||||
|
var hdr_end = null
|
||||||
|
var _do = function() {
|
||||||
|
socket.connect(fd, {address: host, port: port})
|
||||||
|
var body_str = ""
|
||||||
|
if (body != null) {
|
||||||
|
if (is_text(body)) body_str = body
|
||||||
|
else body_str = text(body)
|
||||||
|
}
|
||||||
|
var keys = null
|
||||||
|
var i = 0
|
||||||
|
var req = method + " " + path + " HTTP/1.1" + CRLF
|
||||||
|
req = req + "Host: " + host_port + CRLF
|
||||||
|
req = req + "Connection: close" + CRLF
|
||||||
|
if (headers != null) {
|
||||||
|
keys = array(headers)
|
||||||
|
i = 0
|
||||||
|
while (i < length(keys)) {
|
||||||
|
req = req + keys[i] + ": " + headers[keys[i]] + CRLF
|
||||||
|
i = i + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (length(body_str) > 0) {
|
||||||
|
req = req + "Content-Length: " + text(length(body_str)) + CRLF
|
||||||
|
}
|
||||||
|
req = req + CRLF + body_str
|
||||||
|
socket.send(fd, req)
|
||||||
|
raw = text(socket.recv(fd, 65536))
|
||||||
|
} disruption {
|
||||||
|
raw = null
|
||||||
|
}
|
||||||
|
_do()
|
||||||
|
socket.close(fd)
|
||||||
|
if (raw == null) return null
|
||||||
|
hdr_end = search(raw, CRLF + CRLF)
|
||||||
|
if (hdr_end == null) return null
|
||||||
|
|
||||||
|
var header_text = text(raw, 0, hdr_end)
|
||||||
|
var lines = array(header_text, CRLF)
|
||||||
|
var status_parts = array(lines[0], " ")
|
||||||
|
var status = number(status_parts[1])
|
||||||
|
|
||||||
|
var resp_headers = {}
|
||||||
|
var hi = 1
|
||||||
|
var colon = null
|
||||||
|
while (hi < length(lines)) {
|
||||||
|
colon = search(lines[hi], ": ")
|
||||||
|
if (colon != null) {
|
||||||
|
resp_headers[lower(text(lines[hi], 0, colon))] = text(lines[hi], colon + 2)
|
||||||
|
}
|
||||||
|
hi = hi + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
status: status,
|
||||||
|
headers: resp_headers,
|
||||||
|
body: text(raw, hdr_end + 4)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function close(fd) {
|
function close(fd) {
|
||||||
socket.close(fd)
|
socket.close(fd)
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
serve: serve, accept: accept, on_request: on_request,
|
serve: serve, accept: accept, on_request: on_request,
|
||||||
respond: respond,
|
respond: respond, request: request,
|
||||||
sse_open: sse_open, sse_event: sse_event, sse_close: sse_close,
|
sse_open: sse_open, sse_event: sse_event, sse_close: sse_close,
|
||||||
close: close, fetch: c_http.fetch
|
close: close, fetch: c_http.fetch
|
||||||
}
|
}
|
||||||
|
|||||||
43
probe.ce
43
probe.ce
@@ -6,41 +6,11 @@
|
|||||||
// cell probe <target> <name> k=v ... Query with arguments
|
// cell probe <target> <name> k=v ... Query with arguments
|
||||||
// cell probe --port=8080 game state Use a different port
|
// cell probe --port=8080 game state Use a different port
|
||||||
|
|
||||||
var socket = use('socket')
|
var http = use('http')
|
||||||
var json = use('json')
|
var json = use('json')
|
||||||
|
|
||||||
var host = "127.0.0.1"
|
|
||||||
var port = 9000
|
var port = 9000
|
||||||
|
var base = null
|
||||||
def CRLF = "\r\n"
|
|
||||||
|
|
||||||
function request(method, path, body) {
|
|
||||||
var fd = socket.socket("AF_INET", "SOCK_STREAM")
|
|
||||||
var raw = null
|
|
||||||
var hdr_end = null
|
|
||||||
var _do = function() {
|
|
||||||
socket.connect(fd, {address: host, port: port})
|
|
||||||
var req = method + " " + path + " HTTP/1.1" + CRLF
|
|
||||||
req = req + "Host: " + host + CRLF
|
|
||||||
req = req + "Connection: close" + CRLF
|
|
||||||
if (body != null) {
|
|
||||||
req = req + "Content-Type: application/json" + CRLF
|
|
||||||
req = req + "Content-Length: " + text(length(body)) + CRLF
|
|
||||||
}
|
|
||||||
req = req + CRLF
|
|
||||||
if (body != null) req = req + body
|
|
||||||
socket.send(fd, req)
|
|
||||||
raw = text(socket.recv(fd, 65536))
|
|
||||||
} disruption {
|
|
||||||
raw = null
|
|
||||||
}
|
|
||||||
_do()
|
|
||||||
socket.close(fd)
|
|
||||||
if (raw == null) return null
|
|
||||||
hdr_end = search(raw, CRLF + CRLF)
|
|
||||||
if (hdr_end == null) return null
|
|
||||||
return text(raw, hdr_end + 4)
|
|
||||||
}
|
|
||||||
|
|
||||||
function print_targets(targets) {
|
function print_targets(targets) {
|
||||||
var keys = array(targets)
|
var keys = array(targets)
|
||||||
@@ -106,16 +76,19 @@ function run() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
base = "http://127.0.0.1:" + text(port)
|
||||||
|
|
||||||
var resp = null
|
var resp = null
|
||||||
var body = null
|
var body = null
|
||||||
var data = null
|
var data = null
|
||||||
|
|
||||||
if (target == null) {
|
if (target == null) {
|
||||||
resp = request("GET", "/discover", null)
|
resp = http.request("GET", base + "/discover", null, null)
|
||||||
} else {
|
} else {
|
||||||
body = {target: target, name: name}
|
body = {target: target, name: name}
|
||||||
if (length(array(probe_args)) > 0) body.args = probe_args
|
if (length(array(probe_args)) > 0) body.args = probe_args
|
||||||
resp = request("POST", "/probe", json.encode(body, false))
|
resp = http.request("POST", base + "/probe",
|
||||||
|
{"content-type": "application/json"}, json.encode(body, false))
|
||||||
}
|
}
|
||||||
|
|
||||||
if (resp == null) {
|
if (resp == null) {
|
||||||
@@ -124,7 +97,7 @@ function run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var _parse = function() {
|
var _parse = function() {
|
||||||
data = json.decode(resp)
|
data = json.decode(resp.body)
|
||||||
} disruption {
|
} disruption {
|
||||||
data = null
|
data = null
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user