core/vil/vil_stream_url.cxx
Go to the documentation of this file.
00001 // This is core/vil/vil_stream_url.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author fsm
00008 
00009 #include "vil_stream_url.h"
00010 #include <vil/vil_stream_core.h>
00011 
00012 #include <vcl_cassert.h>
00013 #undef sprintf
00014 #include <vcl_cstdio.h>  // sprintf()
00015 #include <vcl_cstring.h>
00016 #include <vcl_cstdlib.h>
00017 #include <vcl_string.h>
00018 #include <vcl_iostream.h>
00019 #include <vcl_fstream.h>
00020 
00021 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
00022 
00023 # include <unistd.h>       // read(), write(), close()
00024 # include <netdb.h>        // gethostbyname(), sockaddr_in()
00025 # include <sys/socket.h>
00026 # include <netinet/in.h>   // htons()
00027 # ifdef __alpha
00028 #  include <fp.h>           // htons() [ on e.g. DEC alpha, htons is in machine/endian.h]
00029 # endif
00030 # define SOCKET int
00031 #elif defined (VCL_WIN32) && !defined(__CYGWIN__)
00032 # include <winsock2.h>
00033 #endif
00034 
00035 
00036 static const
00037 char base64_encoding[]=
00038 {
00039   'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
00040   'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
00041   'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
00042   'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
00043 };
00044 
00045 static char out_buf[4];
00046 
00047 static const char * encode_triplet(char data[3], unsigned n)
00048 {
00049   assert (n>0 && n <4);
00050   out_buf[0] = base64_encoding[(data[0] & 0xFC) >> 2];
00051 
00052   if (n==1)
00053   {
00054     out_buf[2] = out_buf[3] = '=';
00055     return out_buf;
00056   }
00057 
00058   out_buf[1] = base64_encoding[
00059     ((data[0] & 0x3) << 4) + ((data[1] & 0xf0)>>4)];
00060   out_buf[2] = base64_encoding[
00061     ((data[1] & 0xf) << 2) + ((data[2] & 0xc0)>>6)];
00062 
00063   if (n==2)
00064   {
00065     out_buf[3] = '=';
00066     return out_buf;
00067   }
00068 
00069   out_buf[3] = base64_encoding[ (data[2] & 0x3f) ];
00070   return out_buf;
00071 }
00072 
00073 //=======================================================================
00074 
00075 static vcl_string encode_base64(const vcl_string& in)
00076 {
00077   vcl_string out;
00078     unsigned i = 0, line_octets = 0;
00079   const unsigned l = in.size();
00080   char data[3];
00081     while (i < l)
00082     {
00083     data[0] = in[i++];
00084     data[1] = data[2] = 0;
00085 
00086     if (i == l)
00087     {
00088       out.append(encode_triplet(data,1),4);
00089       return out;
00090     }
00091 
00092     data[1] = in[i++];
00093 
00094     if (i == l)
00095     {
00096       out.append(encode_triplet(data,2),4);
00097       return out;
00098     }
00099 
00100     data[2] = in[i++];
00101 
00102     out.append(encode_triplet(data,3),4);
00103 
00104     if (line_octets >= 68/4) // print carriage return
00105     {
00106       out.append("\r\n",2);
00107       line_octets = 0;
00108     }
00109     else
00110       ++line_octets;
00111     }
00112 
00113     return out;
00114 }
00115 
00116 
00117 vil_stream_url::vil_stream_url(char const *url)
00118   : u_(0)
00119 {
00120   if (vcl_strncmp(url, "http://", 7) != 0)
00121     return; // doesn't look like a URL to me....
00122 
00123   char const *p = url+7;
00124   while (*p && *p!='/')
00125     ++p;
00126 
00127   // split URL into auth, host, path and port number.
00128   vcl_string host = vcl_string(url+7, p);
00129   vcl_string path = (*p) ? p+1 : "";
00130   vcl_string auth;
00131   int port = 80; // default
00132 
00133   // authentication
00134   for (unsigned int i=0; i<host.size(); ++i)
00135     if (host[i] == '@') {
00136       auth = vcl_string(host.c_str(), host.c_str()+i);
00137       host = vcl_string(host.c_str()+i+1, host.c_str() + host.size());
00138       break;
00139     }
00140 
00141   // port?
00142   for (unsigned int i=host.size()-1; i>0; --i)
00143     if (host[i] == ':') {
00144       port = vcl_atoi(host.c_str() + i + 1);
00145       host = vcl_string(host.c_str(), host.c_str() + i);
00146       break;
00147     }
00148 
00149   // do character translation
00150   for (unsigned k =0; k < path.size(); ++k)
00151     if (path[k] == ' ')
00152       path.replace(k, 1, "%20");
00153     else if (path[k] == '%')
00154       path.replace(k, 1, "%25");
00155 
00156   // so far so good.
00157 #ifdef DEBUG
00158   vcl_cerr << "auth = \'" << auth << "\'\n"
00159            << "host = \'" << host << "\'\n"
00160            << "path = \'" << path << "\'\n"
00161            << "port = " << port << vcl_endl;
00162 #endif
00163 
00164 #if defined(VCL_WIN32) && !defined(__CYGWIN__)
00165   static int called_WSAStartup;
00166   if (called_WSAStartup==0)
00167   {
00168     WORD wVersionRequested;
00169     WSADATA wsaData;
00170 
00171     wVersionRequested = MAKEWORD( 2, 2 );
00172 
00173     /* int err = */ WSAStartup( wVersionRequested, &wsaData );
00174   }
00175 #endif
00176 
00177   // create socket endpoint.
00178   SOCKET tcp_socket = socket(PF_INET,      // IPv4 protocols.
00179                              SOCK_STREAM,  // two-way, reliable, connection-based stream socket.
00180                              PF_UNSPEC);   // protocol number.
00181 
00182 #if defined(VCL_WIN32) && !defined(__CYGWIN__)
00183   if (tcp_socket == INVALID_SOCKET) {
00184     vcl_cerr << __FILE__ ": failed to create socket.\n";
00185 # ifndef NDEBUG
00186     vcl_cerr << "error code : " << WSAGetLastError() << vcl_endl;
00187 # endif
00188     return;
00189   }
00190 #else
00191   if (tcp_socket < 0)
00192     vcl_cerr << __FILE__ ": failed to create socket.\n";
00193 #endif
00194 
00195 #ifdef DEBUG
00196   vcl_cerr << __FILE__ ": tcp_sockect = " << tcp_socket << vcl_endl;
00197 #endif
00198 
00199   // get network address of server.
00200   hostent *hp = gethostbyname(host.c_str());
00201   if (! hp) {
00202     vcl_cerr << __FILE__ ": failed to lookup host\n";
00203 #if defined(VCL_WIN32) && !defined(__CYGWIN__)
00204     closesocket(tcp_socket);
00205 #else
00206     close(tcp_socket);
00207 #endif
00208     return;
00209   }
00210 
00211   // make socket address.
00212   sockaddr_in my_addr;
00213   my_addr.sin_family = AF_INET;
00214   my_addr.sin_port = htons(port);  // convert port number to network byte order..
00215   vcl_memcpy(&my_addr.sin_addr, hp->h_addr_list[0], hp->h_length);
00216 
00217   // connect to server.
00218   if (connect(tcp_socket , (sockaddr *) &my_addr, sizeof my_addr) < 0) {
00219     vcl_cerr << __FILE__ ": failed to connect to host\n";
00220     //perror(__FILE__);
00221 #if defined(VCL_WIN32) && !defined(__CYGWIN__)
00222     closesocket(tcp_socket);
00223 #else
00224     close(tcp_socket);
00225 #endif
00226     return;
00227   }
00228 
00229   // buffer for data transfers over socket.
00230 
00231   char buffer[4096];
00232 
00233   // send HTTP 1.1 request.
00234   vcl_snprintf(buffer, 4090, "GET /%s / HTTP/1.1\r\n", path.c_str());
00235   if (auth != "")
00236     vcl_snprintf(buffer+vcl_strlen(buffer), 4090-vcl_strlen(buffer),
00237                  "Authorization:  Basic %s\n", encode_base64(auth).c_str());
00238 
00239   if (vcl_snprintf(buffer+vcl_strlen(buffer), 4090-vcl_strlen(buffer), "\r\n") < 0)
00240   {
00241     vcl_cerr << "ERROR: vil_stream_url buffer overflow.";
00242     vcl_abort();
00243   }
00244 
00245 #if defined(VCL_WIN32) && !defined(__CYGWIN__)
00246   if (send(tcp_socket, buffer, vcl_strlen(buffer), 0) < 0)
00247   {
00248     vcl_cerr << __FILE__ ": error sending HTTP request\n";
00249     closesocket(tcp_socket);
00250     return;
00251   }
00252 #else
00253   if (::write(tcp_socket, buffer, vcl_strlen(buffer)) < 0)
00254   {
00255     vcl_cerr << __FILE__ ": error sending HTTP request\n";
00256     close(tcp_socket);
00257     return;
00258   }
00259 #endif
00260 
00261 
00262 //  vcl_ofstream test2("/test2.jpg", vcl_ios_binary);
00263 
00264   // read from socket into memory.
00265   u_ = new vil_stream_core;
00266   u_->ref();
00267   {
00268     unsigned entity_marker = 0; // count end of header CR and LFs
00269     vil_streampos n;
00270 #if defined(VCL_WIN32) && !defined(__CYGWIN__)
00271     while ((n = recv(tcp_socket, buffer, sizeof buffer,0 )) > 0L)
00272 #else
00273     while ((n = ::read(tcp_socket, buffer, sizeof buffer)) > 0L)
00274 #endif
00275     {
00276       // search for the CRLFCRLF sequence that marks the end
00277       // of the http response header
00278       assert (entity_marker < 5);
00279       if (entity_marker==4)
00280       {
00281         u_->write(buffer, n);
00282 //      test2.write(buffer, n);
00283       }
00284       else
00285       {
00286         for (vil_streampos i=0; i<n; ++i)
00287         {
00288           if ((entity_marker==2||entity_marker==0) && buffer[i]=='\r') entity_marker++;
00289           else if (entity_marker==1 && buffer[i]=='\n') entity_marker++;
00290           else if (entity_marker==3 && buffer[i]=='\n')
00291           {
00292             entity_marker++;
00293             u_->write(buffer+i+1, n-i-1);
00294 //            test2.write(buffer+i+1, n-i-1);
00295             break;
00296           }
00297           else entity_marker=0;
00298         }
00299       }
00300     }
00301   }
00302 
00303 #if 0 // useful for figuring out where the error is
00304   char btest[4096];
00305   vcl_ofstream test("/test.jpg", vcl_ios_binary);
00306   u_->seek(0L);
00307   while (vil_streampos bn = u_->read(btest, 4096L))
00308     test.write(btest, bn);
00309   test.close();
00310 #endif
00311 
00312 
00313   // close connection to server.
00314 #if defined(VCL_WIN32) && !defined(__CYGWIN__)
00315   closesocket(tcp_socket);
00316 #else
00317   close(tcp_socket);
00318 #endif
00319 }
00320 
00321 vil_stream_url::~vil_stream_url()
00322 {
00323   if (u_) {
00324     u_->unref();
00325     u_ = 0;
00326   }
00327 }