// ------------------------------- // // -------- Start of File -------- // // ------------------------------- // // ----------------------------------------------------------- // // C++ Source Code File Name: testprog.cpp // Compiler Used: MSVC40, egcs-2.90.29, HP CPP 10.24 // Produced By: Doug Gaer // File Creation Date: 01/25/2000 // Date Last Modified: 06/21/2000 // Copyright (c) 2000 Dougas M. Gaer // ----------------------------------------------------------- // // ------------- Program Description and Details ------------- // // ----------------------------------------------------------- // /* The VBS C++ classes are copyright (c) 2000, by Douglas M. Gaer. All those who put this code or its derivatives in a commercial product MUST mention this copyright in their documentation for users of the products in which this code or its derivative classes are used. Otherwise, you have the freedom to redistribute verbatim copies of this source code, adapt it to your specific needs, or improve the code and release your improvements to the public provided that the modified files carry prominent notices stating that you changed the files and the date of any change. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE, YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION. This program is used to test the vsbHTTP, HTML, and URL classes. */ // ----------------------------------------------------------- // #include <iostream.h> #include "vbshttp.h" #include "vbshttpc.h" #include "dfileb.h" // Version number and program name const double ProgramVersionNumber = 1033.101; const char *ProgramName = "http testprog"; // Program globals int get_doc_header = 0; // True if request a docment header only int get_document = 0; // True if requesting a document void HelpMessage(const char *program_name, const double version_number) { cout << endl; cout.setf(ios::showpoint | ios::fixed); cout.precision(3); cout << endl; cout << program_name << " version " << version_number << endl; cout << "Usage: " << program_name << " [switches] URL" << endl; cout << "Example: " << program_name << " -h http://www.xyz.com/index.html" << endl; cout << "Switches: " << endl; cout << " -g = Grab the specified file only (default)." << endl; cout << " -h = Get a document header." << endl; cout << endl; } int ProcessArgs(char *arg) { switch(arg[1]) { case 'h': get_doc_header = 1; get_document = 0; break; case 'g': get_doc_header = 0; get_document = 1; break; default: cerr << endl; cerr << "Unknown switch " << arg << endl; cerr << "Exiting..." << endl; cerr << endl; return 0; } arg[0] = '\0'; return 1; // All command line arguments were valid } void PrintHTTPHeader(const vbsHTTPHeader &hdr) { cout << endl; cout << "<------ Document Header ------>" << endl; cout << hdr.http_header; cout << "<----------------------------->" << endl; cout << endl; cout << "Press Enter to continue..." << endl; cin.get(); cout << "Processing the header information..." << endl; cout << vbsHTTPStatusCodeMessage(hdr.http_status) << endl; cout.setf(ios::showpoint | ios::fixed); cout.precision(1); cout << "HTTP version: " << hdr.http_version << endl; cout << "Document status code: " << hdr.http_status << endl; if(hdr.current_server.length() > 0) { cout << "Current Server: " << hdr.current_server.c_str() << endl; } if(hdr.location.length() > 0) { cout << "Location: " << hdr.location.c_str() << endl; } if(hdr.http_last_modified.length() > 0) { cout << "Date Document Last Modified: " << hdr.http_last_modified.c_str() << endl; } if(hdr.date.length() > 0) { cout << "Date: " << hdr.date.c_str() << endl; } if(hdr.http_expires.length() > 0) { cout << "Expires: " << hdr.http_expires.c_str() << endl; } if(hdr.etag.length() > 0) { cout << "Entity tag: " << hdr.etag.c_str() << endl; } if(hdr.authentication_needed) { cout << "Authentication required" << endl; } if(hdr.authentication_scheme.length() > 0) { cout << "Authentication scheme: " << hdr.authentication_scheme.c_str() << endl; } if(hdr.realm.length() > 0) { cout << "Authentication realm: " << hdr.realm.c_str() << endl; } if(hdr.auth_cookie.length() > 0) { cout << "Authentication cookie: " << hdr.auth_cookie.c_str() << endl; } if(hdr.content_encoding.length() > 0) { cout << "Content encoding: " << hdr.content_encoding.c_str() << endl; } if(hdr.pragma.length() > 0) { cout << "Pragma: " << hdr.pragma.c_str() << endl; } if(hdr.cache_control.length() > 0) { cout << "Cache control: " << hdr.cache_control.c_str() << endl; } if(hdr.file_extension.length() > 0) { cout << "File extension: " << hdr.file_extension.c_str() << endl; } if(hdr.length > -1) { cout << "Document length: " << hdr.length << endl; } if(hdr.not_found) { cout << "The requested document was not found" << endl; } if(!hdr.no_cache) { cout << "Using cached copy of the requested document" << endl; } if(hdr.accept_ranges) { cout << "Accepting ranges" << endl; } if(hdr.timeout > -1) { cout << "Timeout: " << hdr.timeout << endl; } if(hdr.max_conns > -1) { cout << "Max connects: " << hdr.max_conns << endl; } if(!hdr.keep_alive) { cout << "The server has closed this connection" << endl; } vbQueue<vbsNetscapeCookie> netscape_cookies(hdr.netscape_cookies); if(!netscape_cookies.IsEmpty()) { cout << endl; cout << "Cookie information. Press Enter to continue..." << endl; cin.get(); while(!netscape_cookies.IsEmpty()) { vbsNetscapeCookie citem; netscape_cookies.Extract(citem); cout << "Hostname: " << citem.host.c_str() << endl; cout << "Name: " << citem.name.c_str() << endl; cout << "Value: " << citem.value.c_str() << endl; if(citem.expires.length() > 0) { cout << "Expires: " << citem.expires.c_str() << endl; } if(citem.domain.length() > 0) { cout << "Domain: " << citem.domain.c_str() << endl; } if(citem.path.length() > 0) { cout << "Path: " << citem.path.c_str() << endl; } if(citem.secure) { cout << "This is a secure cookie" << endl; } if(!netscape_cookies.IsEmpty()) { cout << endl; cout << "Cookie information. Press Enter to continue..." << endl; cin.get(); } } } } void GetDocumentHeader(char *URL) { vbsURL url; vbsURLInfo u; vbString web_url(URL); if(!url.ParseURL(web_url, u)) { cout << "Error parsing url" << endl; return; } vbsHTTPClient httpc; vbsHTTPHeader hdr; if(httpc.RequestHeader(u, hdr) != 0) { cout << httpc.SocketExceptionMessage() << endl; return; } PrintHTTPHeader(hdr); } void DownloadFile(const char *URL) { vbsHTTPClient client; vbsURL url; vbsURLInfo u; if(!url.ParseURL(URL, u)) { cout << "Error parsing URL" << endl; } // Construct the local file name based on the URL information vbString sbuf(u.dir); if(u.file == "?") { // Is this a file or directory while(sbuf.DeleteBeforeIncluding("/")) ; u.file = sbuf; u.dir.DeleteAfterIncluding(sbuf.c_str()); } if(!u.file) { u.file = "index.html"; } // Replace any unsafe characters from the local file name vbString fbuf(u.file); #if defined (__DOS__) || defined (__WIN32__) fbuf.FilterString("?"); #endif DiskFileB outfile(fbuf.c_str(), DiskFileB::df_WRITEONLY, DiskFileB::df_CREATE, DiskFileB::df_TRUNCATE); if(!outfile) { cout << outfile.DiskFileExceptionMessage() << endl; return; } cout << "Connecting to " << u.host << endl; cout << "Grabbing " << u.path << endl; vbsHTTPHeader hdr; if(client.RequestHeader(u, hdr) != 0) { cout << client.SocketExceptionMessage() << endl; return; } if(client.RequestFile(u, hdr, outfile.df_FileStream()) != 0) { cout << client.SocketExceptionMessage() << endl; return; } client.Flush(); } // Program's main thread of execution. // ----------------------------------------------------------- int main(int argc, char **argv) { if(argc < 2) { HelpMessage(ProgramName, ProgramVersionNumber); return 0; } // Process command ling arguments and files int narg; char *arg = argv[narg = 1]; while (narg < argc) { if (arg[0] != '\0') { if (arg[0] == '-') { // Look for command line arguments if(!ProcessArgs(arg)) return 0; // Exit if argument is not valid } else { if(get_document) { cout << endl; cout << "Grabbing: " << arg << endl; DownloadFile((const char *)arg); } else if(get_doc_header) { cout << endl; cout << "Requesting document header: " << arg << endl; GetDocumentHeader(arg); } else { // Default if no argument is specifed cout << endl; cout << "Grabbing: " << arg << endl; DownloadFile((const char *)arg); } } arg = argv[++narg]; } } return 0; } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //