-
-/**
- * If called with "KSFO" loads data from
- * @code
- * http://weather.noaa.gov/pub/data/observations/metar/stations/KSFO.TXT.
- * @endcode
- * Throws sg_io_exception on failure. Gives up after waiting longer than 10 seconds.
- *
- * @param id four-letter ICAO Metar station code, e.g. "KSFO".
- * @param proxy proxy host (optional; default: "")
- * @param port proxy port (optional; default: "80")
- * @param auth proxy authorization information (optional; default: "")
- * @return pointer to Metar data string, allocated by new char[].
- * @see rfc2068.txt for proxy spec ("Proxy-Authorization")
- */
-char *SGMetar::loadData(const char *id, const string& proxy, const string& port,
- const string& auth, time_t time)
-{
- const int buflen = 512;
- char buf[2 * buflen];
-
- string host = proxy.empty() ? "weather.noaa.gov" : proxy;
- string path = "/pub/data/observations/metar/stations/";
-
- path += string(id) + ".TXT";
- _url = "http://weather.noaa.gov" + path;
-
- SGSocket *sock = new SGSocket(host, port.empty() ? "80" : port, "tcp");
- sock->set_timeout(10000);
- if (!sock->open(SG_IO_OUT)) {
- delete sock;
- throw sg_io_exception("cannot connect to " + host);
- }
-
- string get = "GET ";
- if (!proxy.empty())
- get += "http://weather.noaa.gov";
-
- sprintf(buf, "%ld", time);
- get += path + " HTTP/1.0\015\012X-Time: " + buf + "\015\012";
-
- if (!auth.empty())
- get += "Proxy-Authorization: " + auth + "\015\012";
-
- get += "\015\012";
- sock->writestring(get.c_str());
-
- int i;
-
- // skip HTTP header
- while ((i = sock->readline(buf, buflen))) {
- if (i <= 2 && isspace(buf[0]) && (!buf[1] || isspace(buf[1])))
- break;
- if (!strncmp(buf, "X-MetarProxy: ", 9))
- _x_proxy = true;
- }
- if (i) {
- i = sock->readline(buf, buflen);
- if (i)
- sock->readline(&buf[i], buflen);
- }
-
- sock->close();
- delete sock;
-
- char *b = buf;
- scanBoundary(&b);
- if (*b == '<')
- throw sg_io_exception("no metar data available from " + _url);
-
- char *metar = new char[strlen(b) + 2]; // make room for " \0"
- strcpy(metar, b);
- return metar;
-}
-
-