/* * SIPB service monitor: HTTP connection module * * Nickolai Zeldovich * May 2002 */ #include #include #include #include "sipbmon.h" static rxx urx ("^http://([\\w\\.-]+)(:\\d{1,6})?(/\\S*)$", "i"); static void http_read_cb (cbsb cb, int fd, strbuf rbuf) { char buf[1024]; int cc; cc = read (fd, buf, sizeof (buf)); if (cc < 0 && errno != EAGAIN) { fdcb (fd, selread, NULL); close (fd); (*cb) (str ("Error reading response: ") << strerror (errno), false); } if (cc == 0) { fdcb (fd, selread, NULL); close (fd); (*cb) (rbuf, true); } if (cc > 0) rbuf << str (buf, cc); } static void http_write_cb (cbsb cb, int fd, bool ok) { if (!ok) { close (fd); (*cb) (str ("Write failed: ") << strerror (errno), false); return; } strbuf rbuf; fdcb (fd, selread, wrap (&http_read_cb, cb, fd, rbuf)); } static void http_connect_cb (str host, str file, cbsb cb, int fd) { if (fd < 0) { (*cb) (str (strerror (errno)), false); return; } strbuf req; req << "GET " << file << " HTTP/1.0\r\n" << "Host: " << host << "\r\n" << "Connection: close\r\n" << "\r\n"; net_write (fd, req.tosuio (), wrap (&http_write_cb, cb, fd)); } void http_get (str url, cbsb cb) { if (!urx.match (url)) { (*cb) ("Unable to match URL regexp", false); return; } str host = urx[1]; str file = urx[3]; int port; if (urx[2]) convertint (urx[2].cstr () + 1, &port); // skip colon else port = 80; tcpconnect (host, port, wrap (&http_connect_cb, host, file, cb)); }