single_thread.cpp



This file implements the single threaded version of the utility. It can be compiled uner UNIX or Windows.



// -------------------------------------------------------------
// Test program for the class "Http".
//
// Note: Do NOT try to use the following syntax to declare the
// object "http":
//
// Http http();
//
// If you do that, the compiler thinks that you decale a func-
// -tion that takes no argument and returns a object "Http".
// Therfore you get a message related to "non-aggregate" type.
// -------------------------------------------------------------

#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include "http.hpp"

#define NONE -1
#define ALL 0
#define HEADER 1
#define STATUS 2


void print_usage()
{
                cerr << endl
                        << "Usage: http host_address path_to_document port part timeout"
                        << endl
                        << endl
                        << "part could be: all (prints all the data from the HTTP server)" << endl
                        << " header (prints only the header)" << endl
                        << " status (prints only the status)" << endl;
}



int main (int argc, char *argv[])
{
        Http http;
        int opt;

        if (argc != 6)
        {
                print_usage();
                return 1;
        }

        opt = NONE;
        if (strcmp(argv[4], "all") == 0) { opt = ALL; }
        if (strcmp(argv[4], "header") == 0) { opt = HEADER; }
        if (strcmp(argv[4], "status") == 0) { opt = STATUS; }

        if (opt == NONE)
        {
                cout << endl << "ERROR: Invalid part";
                return 1;
        }

// -------------------------------------------------------------
// Initialilze connexion's parameters
// -------------------------------------------------------------

        try {
                        http.set_hostname(argv[1]);
                        http.set_url(argv[2]);
         }

        catch ( Http_memory_error e )
        {
                cout << endl << "ERROR: Init(): Can not allocate memory !" << endl;
                return 1;
        }

        http.set_port(atoi(argv[3]));
        http.set_timeout(atoi(argv[5]));

// -------------------------------------------------------------
// Get the document via HTTP
// -------------------------------------------------------------

        try { http.get(); }

        catch ( Http_memory_error e )
        {
                cout << endl << "ERROR: get(): Can not allocate memory !" << endl;
                return 1;
        }
        
        catch ( Http_invalid_args e )
        {
                cout << endl << "ERROR: get(): Invalid connexion arguments !" << endl;
                return 1;
        }
                
        catch ( Http_connect_failed e )
        {
                cout << endl << "ERROR: get(): Can not connect to the server !" << endl;
                switch (e.get_error())
                {
                        case SCK_CREATE_ERROR:
                                cout << endl << "ERROR: Can not create socket";
                                break;
                        case SCK_CONNECT_ERROR:
                                cout << endl << "ERROR: Can connect socket to server";
                                break;
                        case SCK_GET_IP_ERROR:
                                cout << endl << "ERROR: Can not resolve name";
                                break;
                        case SCK_SOCK_INIT_DLL_ERR:
                                cout << endl << "ERROR: Can not initialize the Winsock DLL";
                                break;
                        case SCK_SOCK_INIT_DLL_CHECK_ERR:
                                cout << endl << "ERROR: Invalid WinSock verion";
                                break;
                        case SCK_SET_NON_BLOCK_ERR:
                                cout << endl << "ERROR: Can not set the socket in non blocking mode";
                                break;
                        case SCK_SET_BLOCK_ERR:
                                cout << endl << "ERROR: Can not set the socket in blocking mode";
                                break;
                        default:
                                cout << endl << "ERROR: Unknown error (" << e.get_error() << ")";
                }
                return 1;
        }

        catch ( Http_write_failed e )
        {
                cout << endl << "ERROR: get(): write failed !" << endl;
                return 1;
        }

        catch ( Http_read_failed e )
        {
                cout << endl << "ERROR: get(): read failed !" << endl;
                return 1;
        }

        catch ( Http_timeout_connect e )
        {
                cout << endl << "ERROR: get(): timeout - can not connet to the host" << endl;
                return 1;
        }

    catch ( Http_timeout_read e )
    {
         cout << endl << "ERROR: get(): timeout - the server did not send me the document" << endl;
         return 1;
    }

        catch ( Http_timer_error e )
    {
         cout << endl << "ERROR: get(): can not set tirmer" << endl;
         return 1;
    }

// -------------------------------------------------------------
// Print all the file
// -------------------------------------------------------------

        if (opt == ALL)
        {
                cout << endl
                        << http.get_data()
                        << endl;
                return 0;
        }


// -------------------------------------------------------------
// Print the header
// -------------------------------------------------------------

        if (opt == HEADER)
        {
                char* header;
                int hs;

                hs = http.get_header(&header);
                if (hs == -1)
                {
                        cout << endl << "ERROR: Can not extract header from data" << endl;
                        return 1;
                }

                cout << endl
                        << header
                        << endl;

                delete [] header;
                return 0;
        }

// -------------------------------------------------------------
// Print the status
// -------------------------------------------------------------

        if (opt == STATUS)
        {
                char* status;
                int hs;

                hs = http.get_status(&status);
                if (hs == -1)
                {
                        cout << endl << "ERROR: Can not extract status from data" << endl;
                        return 1;
                }

                cout << endl
                        << status
                        << endl;

                delete [] status;
                return 0;
        }

        return 0;
}