#include #include #include #include #include #include "../tlmplib/tlmplib.h" #include "tlmpnet.h" #include "tlmpnet.m" class _F_http_get_private{ }; void _F_http_get::fail(const HTTP_HEADER_INFO &info, const char *msg) { tlmp_error (MSG_U(E_HTTPCON,"http_get: %s while procsssing %s\n") ,msg,info.url.get()); } void _F_http_get::end(const HTTP_HEADER_INFO &info) { } void _F_http_get::header(const char *line, int len, const HTTP_HEADER_INFO &info, bool &end) { } /* Parse a URL into a host, a port and a file path */ static int http_parseurl ( const char *url, const char *current_host, SSTRING &proto, SSTRING &host, SSTRING &port, SSTRING &path) { int ret = 0; const char *start = url; while (isalpha(*url)) url++; proto = "http"; port = "80"; host.setempty(); if (url > start && strncmp(url,"://",3)==0){ proto.setfrom (start,url-start); url+=3; } start = url; while (*url != '\0' && *url != '/' && *url != ':') url++; if (url > start){ host.setfrom (start,url-start); }else{ ret = -1; } if (*url == ':'){ url++; start = url; while (*url != '\0' && *url != '/') url++; if (url > start){ port.setfrom (start,url-start); }else{ ret = -1; } } path.setfrom (url); return ret; } /* Perform an http request */ int http_get ( _F_http_get &c, const char *url, const char *realm, const char *user, const char *passwd) { _F_http_get_private priv; c.priv = &priv; glocal int ret = -1; glocal _F_http_get *c = &c; glocal HTTP_HEADER_INFO info; glocal const char *realm = realm; glocal const char *user = user; glocal const char *passwd = passwd; glocal.info.url = url; if(http_parseurl (url,"",glocal.info.proto,glocal.info.host ,glocal.info.port,glocal.info.path)==-1){ c.fail (glocal.info,MSG_U(E_IVLDURL,"Invalid URL")); }else{ glocal bool in_header = true; glocal int content_length = 0; glocal int content_rec = 0; (glocal.info.host.get(),glocal.info.port.get(),100); sendf ("GET %s HTTP/1.0\r\n",glocal.info.path.get()); send ("User-Agent: Mozilla/5.0 (compatible; Konqueror/3; Linux)\r\n"); SSTRING tmp; tmp.setfromf ("%s:%s",glocal.user,glocal.passwd); char *pt = base64_encode (tmp.get(),tmp.getlen()); sendf ("Authorization: Basic %s\r\n",pt); free (pt); sendf ("\r\n"); if (glocal.in_header){ if (line[0] == '\0' || line[0] == '\r'){ glocal.in_header = false; }else{ if (strncasecmp(line,"Content-Length:",15)==0){ glocal.content_length = atoi(str_skip(line+15)); } glocal.c->header (line,info.linelen,glocal.info,end); glocal.info.header.add (line); } }else{ glocal.c->oneline (line,info.linelen,glocal.info,end); glocal.content_rec += info.linelen+1; if (glocal.content_rec >= glocal.content_length) end = true; } glocal.c->fail(glocal.info,MSG_U(E_CONFAIL,"Connection failure")); } return glocal.ret; } int http_get (_F_http_get &c, const char *url) { return http_get (c,url,NULL,NULL,NULL); }