|
|
@@ -1,16 +1,12 @@
|
|
|
-/* curl: fetch one http:// URL with a minimal HTTP/1.0 GET and dump the
|
|
|
- * status line, the headers and the body to the terminal.
|
|
|
+/* curl: ring-3 command. Fetch one http:// URL with a minimal HTTP/1.0 GET and
|
|
|
+ * dump the status line, the headers and the body to the terminal.
|
|
|
*
|
|
|
* Deliberately small: no TLS, no redirect following (a Location header is
|
|
|
- * reported), no gzip; a chunked response is decoded. Uses the DNS resolver
|
|
|
- * (dns.c) and the TCP client (tcp.c); the NIC is owned for the whole run. */
|
|
|
+ * reported), no gzip; a chunked response is decoded. The TCP client (tcp.c)
|
|
|
+ * and the DNS resolver stay in the kernel; the nic is owned for the run
|
|
|
+ * (SYS_NET_ACQUIRE) and TCP goes through the SYS_TCP_* calls. */
|
|
|
|
|
|
-#include "io.h"
|
|
|
-#include "timer.h"
|
|
|
-#include "netdev.h"
|
|
|
-#include "net.h"
|
|
|
-#include "dns.h"
|
|
|
-#include "tcp.h"
|
|
|
+#include "ucmd_lib.h"
|
|
|
|
|
|
#define URL_HOST_MAX 128
|
|
|
#define URL_PATH_MAX 128
|
|
|
@@ -27,36 +23,6 @@ struct url {
|
|
|
unsigned int port;
|
|
|
};
|
|
|
|
|
|
-/* ---- small print helpers ---- */
|
|
|
-
|
|
|
-static void put_dec(unsigned int v)
|
|
|
-{
|
|
|
- char b[12];
|
|
|
- int i = 0;
|
|
|
-
|
|
|
- if (v == 0) {
|
|
|
- io_putchar('0');
|
|
|
- return;
|
|
|
- }
|
|
|
- while (v) {
|
|
|
- b[i++] = (char)('0' + (v % 10));
|
|
|
- v /= 10;
|
|
|
- }
|
|
|
- while (i)
|
|
|
- io_putchar(b[--i]);
|
|
|
-}
|
|
|
-
|
|
|
-static void put_ip(const unsigned char *ip)
|
|
|
-{
|
|
|
- unsigned int i;
|
|
|
-
|
|
|
- for (i = 0; i < 4; i++) {
|
|
|
- if (i)
|
|
|
- io_putchar('.');
|
|
|
- put_dec(ip[i]);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
static int starts_with(const char *s, const char *prefix)
|
|
|
{
|
|
|
unsigned int i = 0;
|
|
|
@@ -169,7 +135,6 @@ static unsigned int req_app_dec(char *buf, unsigned int pos, unsigned int v)
|
|
|
/* ---- HTTP response reader (a refillable line/byte stream) ---- */
|
|
|
|
|
|
struct reader {
|
|
|
- struct tcp_conn *conn;
|
|
|
unsigned char buf[READER_MAX];
|
|
|
unsigned int len, pos;
|
|
|
unsigned int total_deadline;
|
|
|
@@ -180,16 +145,18 @@ struct reader {
|
|
|
/* Returns 1 when new data (or EOF) is available, 0 on timeout/reset. */
|
|
|
static int reader_fill(struct reader *r)
|
|
|
{
|
|
|
- unsigned int deadline, idle;
|
|
|
+ unsigned int now, idle, deadline, rel;
|
|
|
int n;
|
|
|
|
|
|
if (r->eof)
|
|
|
return 0;
|
|
|
|
|
|
- idle = get_ticks_ms() + IDLE_TIMEOUT_MS;
|
|
|
+ now = u_ticks_ms();
|
|
|
+ idle = now + IDLE_TIMEOUT_MS;
|
|
|
deadline = idle < r->total_deadline ? idle : r->total_deadline;
|
|
|
+ rel = (int)(deadline - now) > 0 ? deadline - now : 1;
|
|
|
|
|
|
- n = tcp_recv(r->conn, r->buf, sizeof(r->buf), deadline);
|
|
|
+ n = u_syscall3(SYS_TCP_RECV, (int)r->buf, sizeof(r->buf), (int)rel);
|
|
|
if (n > 0) {
|
|
|
r->len = (unsigned int)n;
|
|
|
r->pos = 0;
|
|
|
@@ -341,7 +308,7 @@ static unsigned int dump_body(struct reader *r, int have_limit,
|
|
|
break;
|
|
|
if (c == '\r')
|
|
|
continue;
|
|
|
- io_putchar((char)c);
|
|
|
+ u_putc((char)c);
|
|
|
n++;
|
|
|
}
|
|
|
return n;
|
|
|
@@ -375,7 +342,7 @@ static unsigned int dump_chunked(struct reader *r)
|
|
|
k++;
|
|
|
}
|
|
|
if (k == 0) {
|
|
|
- io_putstr("curl: bad chunk size\n");
|
|
|
+ u_puts("curl: bad chunk size\n");
|
|
|
return total;
|
|
|
}
|
|
|
if (sz == 0) {
|
|
|
@@ -389,176 +356,176 @@ static unsigned int dump_chunked(struct reader *r)
|
|
|
|
|
|
if (c < 0)
|
|
|
return total;
|
|
|
- io_putchar((char)c);
|
|
|
+ u_putc((char)c);
|
|
|
total++;
|
|
|
}
|
|
|
reader_readline(r, line, sizeof(line)); /* the CRLF */
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-/* ---- the command ---- */
|
|
|
-
|
|
|
-void program_curl_main(const char *arg)
|
|
|
+void _start(const char *arg)
|
|
|
{
|
|
|
- netdev_acquire();
|
|
|
- {
|
|
|
- static struct url u;
|
|
|
- static char req[REQ_MAX];
|
|
|
- static struct reader r;
|
|
|
- const struct netdev *nd = netdev_active();
|
|
|
- unsigned char ip[4], hop[4], mac[6];
|
|
|
- struct tcp_conn conn;
|
|
|
- char line[LINE_MAX_C];
|
|
|
- char location[128];
|
|
|
- unsigned int n, status = 0, body = 0, content_len = 0;
|
|
|
- int have_loc = 0, have_len = 0, chunked = 0, rv;
|
|
|
-
|
|
|
- if (arg[0] == '\0') {
|
|
|
- io_putstr("curl: usage curl <http://host[:port]/[path]>\n");
|
|
|
- goto out;
|
|
|
- }
|
|
|
- rv = url_parse(arg, &u);
|
|
|
- if (rv == -2) {
|
|
|
- io_putstr("curl: https is not supported (no TLS)\n");
|
|
|
- goto out;
|
|
|
- }
|
|
|
- if (rv != 0) {
|
|
|
- io_putstr("curl: bad URL\n");
|
|
|
- goto out;
|
|
|
- }
|
|
|
- if (!nd) {
|
|
|
- io_putstr("curl: no NIC found\n");
|
|
|
- goto out;
|
|
|
- }
|
|
|
- if (!net_is_configured()) {
|
|
|
- io_putstr("curl: no IP address (run dhcp or netip)\n");
|
|
|
- goto out;
|
|
|
- }
|
|
|
+ static struct url u;
|
|
|
+ static char req[REQ_MAX];
|
|
|
+ static struct reader r;
|
|
|
+ struct sys_netinfo ni;
|
|
|
+ unsigned char ip[4], hop[4], mac[6];
|
|
|
+ char line[LINE_MAX_C];
|
|
|
+ char location[128];
|
|
|
+ unsigned int n, status = 0, body = 0, content_len = 0;
|
|
|
+ int have_loc = 0, have_len = 0, chunked = 0, rv;
|
|
|
+
|
|
|
+ if (!arg || !arg[0]) {
|
|
|
+ u_puts("curl: usage curl <http://host[:port]/[path]>\n");
|
|
|
+ u_exit(1);
|
|
|
+ }
|
|
|
|
|
|
- io_putstr("curl: GET http://");
|
|
|
- io_putstr(u.host);
|
|
|
- if (u.port != 80) {
|
|
|
- io_putstr(":");
|
|
|
- put_dec(u.port);
|
|
|
- }
|
|
|
- io_putstr(u.path);
|
|
|
- io_putchar('\n');
|
|
|
+ rv = url_parse(arg, &u);
|
|
|
+ if (rv == -2) {
|
|
|
+ u_puts("curl: https is not supported (no TLS)\n");
|
|
|
+ u_exit(1);
|
|
|
+ }
|
|
|
+ if (rv != 0) {
|
|
|
+ u_puts("curl: bad URL\n");
|
|
|
+ u_exit(1);
|
|
|
+ }
|
|
|
+ if (u_syscall1(SYS_NET_INFO, (int)&ni) != 1) {
|
|
|
+ u_puts("curl: no NIC found\n");
|
|
|
+ u_exit(1);
|
|
|
+ }
|
|
|
+ if (u_syscall0(SYS_NET_CONFIGURED) != 1) {
|
|
|
+ u_puts("curl: no IP address (run dhcp or netip)\n");
|
|
|
+ u_exit(1);
|
|
|
+ }
|
|
|
|
|
|
- if (net_parse_ip(u.host, ip) != 0) {
|
|
|
- if (dns_resolve(u.host, ip) != 0)
|
|
|
- goto out;
|
|
|
- }
|
|
|
+ u_puts("curl: GET http://");
|
|
|
+ u_puts(u.host);
|
|
|
+ if (u.port != 80) {
|
|
|
+ u_puts(":");
|
|
|
+ u_putdec(u.port);
|
|
|
+ }
|
|
|
+ u_puts(u.path);
|
|
|
+ u_putc('\n');
|
|
|
|
|
|
- net_next_hop(ip, hop);
|
|
|
- if (net_arp_resolve(hop, mac) != 0) {
|
|
|
- io_putstr("curl: no ARP reply from ");
|
|
|
- put_ip(hop);
|
|
|
- io_putchar('\n');
|
|
|
- goto out;
|
|
|
- }
|
|
|
+ if (u_syscall0(SYS_NET_ACQUIRE) != 0) {
|
|
|
+ u_puts("curl: NIC busy\n");
|
|
|
+ u_exit(1);
|
|
|
+ }
|
|
|
|
|
|
- rv = tcp_connect(mac, ip, u.port, &conn);
|
|
|
- if (rv == -1) {
|
|
|
- io_putstr("curl: connect timeout\n");
|
|
|
- goto out;
|
|
|
- }
|
|
|
- if (rv == -2) {
|
|
|
- io_putstr("curl: connection refused\n");
|
|
|
- goto out;
|
|
|
- }
|
|
|
- if (rv != 0) {
|
|
|
- io_putstr("curl: connect failed\n");
|
|
|
+ if (u_parse_ip(u.host, ip) != 0) {
|
|
|
+ if (u_syscall2(SYS_DNS_RESOLVE, (int)u.host, (int)ip) != 0)
|
|
|
goto out;
|
|
|
- }
|
|
|
- io_putstr("curl: connected, sending request\n");
|
|
|
-
|
|
|
- n = req_app(req, 0, "GET ");
|
|
|
- n = req_app(req, n, u.path);
|
|
|
- n = req_app(req, n, " HTTP/1.0\r\nHost: ");
|
|
|
- n = req_app(req, n, u.host);
|
|
|
- if (u.port != 80) {
|
|
|
- n = req_app(req, n, ":");
|
|
|
- n = req_app_dec(req, n, u.port);
|
|
|
- }
|
|
|
- n = req_app(req, n, "\r\nUser-Agent: nebos-curl/0.1\r\n"
|
|
|
- "Accept: */*\r\nConnection: close\r\n\r\n");
|
|
|
- if (n > conn.peer_mss) {
|
|
|
- io_putstr("curl: request too long\n");
|
|
|
- goto out;
|
|
|
- }
|
|
|
- rv = tcp_send(&conn, req, n);
|
|
|
- if (rv != 0) {
|
|
|
- io_putstr("curl: request send failed\n");
|
|
|
- goto out;
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- r.conn = &conn;
|
|
|
- r.len = 0;
|
|
|
- r.pos = 0;
|
|
|
- r.eof = 0;
|
|
|
- r.error = 0;
|
|
|
- r.total_deadline = get_ticks_ms() + TOTAL_TIMEOUT_MS;
|
|
|
+ u_syscall2(SYS_NET_NEXT_HOP, (int)ip, (int)hop);
|
|
|
+ rv = u_syscall2(SYS_NET_ARP, (int)hop, (int)mac);
|
|
|
+ if (rv != 0) {
|
|
|
+ u_puts("curl: no ARP reply from ");
|
|
|
+ u_put_ip(hop);
|
|
|
+ u_putc('\n');
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
|
|
|
+ rv = u_syscall3(SYS_TCP_CONNECT, (int)mac, (int)ip, (int)u.port);
|
|
|
+ if (rv == -1) {
|
|
|
+ u_puts("curl: connect timeout\n");
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
+ if (rv == -2) {
|
|
|
+ u_puts("curl: connection refused\n");
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
+ if (rv != 0) {
|
|
|
+ u_puts("curl: connect failed\n");
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
+ u_puts("curl: connected, sending request\n");
|
|
|
+
|
|
|
+ n = req_app(req, 0, "GET ");
|
|
|
+ n = req_app(req, n, u.path);
|
|
|
+ n = req_app(req, n, " HTTP/1.0\r\nHost: ");
|
|
|
+ n = req_app(req, n, u.host);
|
|
|
+ if (u.port != 80) {
|
|
|
+ n = req_app(req, n, ":");
|
|
|
+ n = req_app_dec(req, n, u.port);
|
|
|
+ }
|
|
|
+ n = req_app(req, n, "\r\nUser-Agent: nebos-curl/0.1\r\n"
|
|
|
+ "Accept: */*\r\nConnection: close\r\n\r\n");
|
|
|
+ if ((int)n > u_syscall0(SYS_TCP_PEER_MSS)) {
|
|
|
+ u_puts("curl: request too long\n");
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
+ rv = u_syscall2(SYS_TCP_SEND, (int)req, n);
|
|
|
+ if (rv != 0) {
|
|
|
+ u_puts("curl: request send failed\n");
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
+
|
|
|
+ r.len = 0;
|
|
|
+ r.pos = 0;
|
|
|
+ r.eof = 0;
|
|
|
+ r.error = 0;
|
|
|
+ r.total_deadline = u_ticks_ms() + TOTAL_TIMEOUT_MS;
|
|
|
+
|
|
|
+ rv = reader_readline(&r, line, sizeof(line));
|
|
|
+ if (rv < 0) {
|
|
|
+ u_puts("curl: no response (");
|
|
|
+ u_puts(r.error == -2 ? "reset" : "timeout");
|
|
|
+ u_puts(")\n");
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
+ status = parse_status(line);
|
|
|
+ u_puts("curl: ");
|
|
|
+ u_puts(line);
|
|
|
+ u_putc('\n');
|
|
|
+
|
|
|
+ for (;;) {
|
|
|
rv = reader_readline(&r, line, sizeof(line));
|
|
|
if (rv < 0) {
|
|
|
- io_putstr("curl: no response (");
|
|
|
- io_putstr(r.error == -2 ? "reset" : "timeout");
|
|
|
- io_putstr(")\n");
|
|
|
+ u_puts("curl: connection lost in headers\n");
|
|
|
goto out;
|
|
|
}
|
|
|
- status = parse_status(line);
|
|
|
- io_putstr("curl: ");
|
|
|
- io_putstr(line);
|
|
|
- io_putchar('\n');
|
|
|
-
|
|
|
- for (;;) {
|
|
|
- rv = reader_readline(&r, line, sizeof(line));
|
|
|
- if (rv < 0) {
|
|
|
- io_putstr("curl: connection lost in headers\n");
|
|
|
- goto out;
|
|
|
- }
|
|
|
- if (rv == 0)
|
|
|
- break; /* end of headers */
|
|
|
- io_putstr(line);
|
|
|
- io_putchar('\n');
|
|
|
- if (hdr_match(line, "content-length"))
|
|
|
- have_len = parse_dec_after_colon(line, &content_len);
|
|
|
- else if (hdr_match(line, "transfer-encoding") &&
|
|
|
- contains_ci(line, "chunked"))
|
|
|
- chunked = 1;
|
|
|
- else if (hdr_match(line, "location")) {
|
|
|
- copy_after_colon(line, location,
|
|
|
- sizeof(location));
|
|
|
- have_loc = 1;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (status >= 300 && status < 400 && have_loc) {
|
|
|
- io_putstr("curl: redirect not followed: ");
|
|
|
- io_putstr(location);
|
|
|
- io_putchar('\n');
|
|
|
+ if (rv == 0)
|
|
|
+ break; /* end of headers */
|
|
|
+ u_puts(line);
|
|
|
+ u_putc('\n');
|
|
|
+ if (hdr_match(line, "content-length"))
|
|
|
+ have_len = parse_dec_after_colon(line, &content_len);
|
|
|
+ else if (hdr_match(line, "transfer-encoding") &&
|
|
|
+ contains_ci(line, "chunked"))
|
|
|
+ chunked = 1;
|
|
|
+ else if (hdr_match(line, "location")) {
|
|
|
+ copy_after_colon(line, location, sizeof(location));
|
|
|
+ have_loc = 1;
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- io_putchar('\n');
|
|
|
- if (chunked)
|
|
|
- body = dump_chunked(&r);
|
|
|
- else
|
|
|
- body = dump_body(&r, have_len, content_len);
|
|
|
- io_putchar('\n');
|
|
|
-
|
|
|
- if (r.error == -2)
|
|
|
- io_putstr("curl: connection reset\n");
|
|
|
- else if (r.error == -1)
|
|
|
- io_putstr("curl: timeout while reading body\n");
|
|
|
-
|
|
|
- io_putstr("curl: ");
|
|
|
- put_dec(body);
|
|
|
- io_putstr(" bytes received, status ");
|
|
|
- put_dec(status);
|
|
|
- io_putstr("\n");
|
|
|
-
|
|
|
- tcp_close(&conn);
|
|
|
+ if (status >= 300 && status < 400 && have_loc) {
|
|
|
+ u_puts("curl: redirect not followed: ");
|
|
|
+ u_puts(location);
|
|
|
+ u_putc('\n');
|
|
|
}
|
|
|
+
|
|
|
+ u_putc('\n');
|
|
|
+ if (chunked)
|
|
|
+ body = dump_chunked(&r);
|
|
|
+ else
|
|
|
+ body = dump_body(&r, have_len, content_len);
|
|
|
+ u_putc('\n');
|
|
|
+
|
|
|
+ if (r.error == -2)
|
|
|
+ u_puts("curl: connection reset\n");
|
|
|
+ else if (r.error == -1)
|
|
|
+ u_puts("curl: timeout while reading body\n");
|
|
|
+
|
|
|
+ u_puts("curl: ");
|
|
|
+ u_putdec(body);
|
|
|
+ u_puts(" bytes received, status ");
|
|
|
+ u_putdec(status);
|
|
|
+ u_putc('\n');
|
|
|
+
|
|
|
+ u_syscall0(SYS_TCP_CLOSE);
|
|
|
out:
|
|
|
- netdev_release();
|
|
|
+ u_syscall0(SYS_NET_RELEASE);
|
|
|
+ u_exit(0);
|
|
|
}
|