Listing 3: How to use CookieManager in an application

//  CookieTest.cpp: 

#include "CookieManager.h"
#include <iostream>

using std::cout;

int main(int argc, char* argv[])
{
    // Test Cases based on the Examples given in the Specifications
    // More Test Cases are available in the CookieTest1.cpp on the 
    // ftp site
    bool bSecure = false;
    string strCookie;

    // Version 0 Specification (Netscape Specification)
    cout << "-- Version 0 Specification (Netscape Specification) --";
    cout << "\nExample 1 \n";

    // Example 1.1
    CookieManager objOldSpecMgr("www.foo.com");
    strCookie = "Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/; " +
        string("expires=Friday, 09-Nov-2001 23:12:12 GMT");
    cout << "Adding : " << strCookie << "\n";
    objOldSpecMgr.addCookie (strCookie);

    // Results
    cout << "Result - Request Path=/ : \n";
    cout << objOldSpecMgr.getCookieString ("/", bSecure);

    // Example 1.2
    strCookie = 
        "Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/";
    cout << "\nAdding : " << strCookie << "\n";
    objOldSpecMgr.addCookie (strCookie);
    
    // Results
    cout << "Result - Request Path=/ : \n";
    cout << objOldSpecMgr.getCookieString ("/", bSecure) << "\n";

    // Example 1.3
    strCookie = "Set-Cookie: SHIPPING=FEDEX; path=/foo";
    cout << "Adding : " << strCookie << "\n";
    objOldSpecMgr.addCookie (strCookie);

    // Results
    cout << "Result - Request Path=/ : \n";
    cout << objOldSpecMgr.getCookieString ("/", bSecure) << "\n";
    cout << "Result - Requst Path = /foo : \n";
    cout << objOldSpecMgr.getCookieString ("/foo", bSecure) << "\n";

    // Example 2.1
    cout << " Example 2 \n";
    objOldSpecMgr.clearAll();
    strCookie = 
        "Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/";
    cout << "Adding : " << strCookie << "\n";
    objOldSpecMgr.addCookie (strCookie);

    // Results
    cout << "Result - Request Path=/ : \n";
    cout << objOldSpecMgr.getCookieString ("/", bSecure) << "\n";
    
    // Example 2.2
    strCookie = 
        "Set-Cookie: PART_NUMBER=RIDING_ROCKET_0023; path=/ammo";
    cout << "Adding : " << strCookie << "\n";
    objOldSpecMgr.addCookie (strCookie);

    // Results
    cout << "Result - Request Path=/ammo : \n";
    cout << objOldSpecMgr.getCookieString ("/ammo", bSecure) << "\n";

    //-------------------------------------------------------------//
    // RFC 2109 Specification
    cout << "\n\n-----  RFC 2109 Specification ----- " << "\n";
    CookieManager objRFC2109Mgr;
    objRFC2109Mgr.setDomain("www.acme.com");
    cout << " Example 1 \n";

    // Example 1.1
    strCookie = 
        "Set-Cookie: CUSTOMER=\"WILE_E_COYOTE\"; Version=\"1\"; " +
        string("Path=\"/acme\"");
    cout << "Adding : " << strCookie << "\n";
    objRFC2109Mgr.addCookie (strCookie);

    // Results
    cout << "Result - Request Path=/acme/pickitem : \n";
    cout << objRFC2109Mgr.getCookieString 
        ("/acme/pickitem", bSecure) << "\n";
    
    // Example 1.2
    strCookie = 
        "Set-Cookie: Part_Number=\"Rocket_Launcher_0001\"; " +
        string("Version=\"1\"; Path=\"/acme\"");
    cout << "Adding : " << strCookie << "\n";
    objRFC2109Mgr.addCookie (strCookie);

    // Results
    cout << "Result - Request Path=/acme/shipping : \n";
    cout << objRFC2109Mgr.getCookieString 
        ("/acme/shipping", bSecure) << "\n";

    // Example 1.3
    strCookie = "Set-Cookie: Shipping=\"FedEx\"; Version=\"1\"; " +
        string("Path=\"/acme\"");
    cout << "Adding : " << strCookie << "\n";
    objRFC2109Mgr.addCookie (strCookie);

    // Results
    cout << "Result - Request Path=/acme/process : \n";
    cout << objRFC2109Mgr.getCookieString 
        ("/acme/process", bSecure) << "\n";

    // Example 2
    cout << " Example 1 \n";
    objRFC2109Mgr.clearAll();

    // Example 2.1
    strCookie = 
        "Set-Cookie: Part_Number=\"Rocket_Launcher_0001\"; " +
        string("Version=\"1\"; Path=\"/acme\"");
    cout << "Adding : " << strCookie << "\n";
    objRFC2109Mgr.addCookie (strCookie);
    strCookie = "Set-Cookie: Part_Number=\"Riding_Rocket_0023\"; " +
        string("Version=\"1\"; Path=\"/acme/ammo\"");
    cout << "Adding : " << strCookie << "\n";
    objRFC2109Mgr.addCookie (strCookie);

    // Results
    cout << "Result - Request Path=/acme/ammo/pickitem : \n";
    cout << objRFC2109Mgr.getCookieString 
        ("/acme/ammo/pickitem", bSecure) << "\n";
    cout << "Result - Request Path=/acme/parts/ : \n";
    cout << objRFC2109Mgr.getCookieString 
        ("/acme/parts/", bSecure) << "\n";

    return 0;
}


// Output:

-- Version 0 Specification (Netscape Specification) --
Example 1 
Adding : Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/; 
            expires=Friday, 09-Nov-2001 23:12:12 GMT
Result - Request Path=/ : 
Cookie: CUSTOMER=WILE_E_COYOTE


Adding : Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/
Result - Request Path=/ : 
Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001


Adding : Set-Cookie: SHIPPING=FEDEX; path=/foo
Result - Request Path=/ : 
Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001


Result - Requst Path = /foo : 
Cookie: SHIPPING=FEDEX; CUSTOMER=WILE_E_COYOTE; 
        PART_NUMBER=ROCKET_LAUNCHER_0001


 Example 2 
Adding : Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/
Result - Request Path=/ : 
Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001


Adding : Set-Cookie: PART_NUMBER=RIDING_ROCKET_0023; path=/ammo
Result - Request Path=/ammo : 
Cookie: PART_NUMBER=RIDING_ROCKET_0023; 
    PART_NUMBER=ROCKET_LAUNCHER_0001




-----  RFC 2109 Specification ----- 
 Example 1 
Adding : Set-Cookie: CUSTOMER="WILE_E_COYOTE"; Version="1"; 
    Path="/acme"
Result - Request Path=/acme/pickitem : 
Cookie: $Version="1"; CUSTOMER="WILE_E_COYOTE"; $Path="/acme"


Adding : Set-Cookie: Part_Number="Rocket_Launcher_0001"; Version="1"; 
        Path="/acme"
Result - Request Path=/acme/shipping : 
Cookie: $Version="1"; CUSTOMER="WILE_E_COYOTE"; $Path="/acme"; 
         Part_Number="Rocket_Launcher_0001"; $Path="/acme"


Adding : Set-Cookie: Shipping="FedEx"; Version="1"; Path="/acme"
Result - Request Path=/acme/process : 
Cookie: $Version="1"; CUSTOMER="WILE_E_COYOTE"; $Path="/acme"; 
        Part_Number="Rocket_Launcher_0001"; $Path="/acme"; 
        Shipping="FedEx"; $Path="/acme"


 Example 2 
Adding : Set-Cookie: Part_Number="Rocket_Launcher_0001"; 
            Version="1"; Path="/acme"
Adding : Set-Cookie: Part_Number="Riding_Rocket_0023"; Version="1";
            Path="/acme/ammo"
Result - Request Path=/acme/ammo/pickitem : 
Cookie: $Version="1"; Part_Number="Riding_Rocket_0023"; 
    $Path="/acme/ammo"; 
        Part_Number="Rocket_Launcher_0001"; $Path="/acme"


Result - Request Path=/acme/parts/ : 
Cookie: $Version="1"; Part_Number="Rocket_Launcher_0001"; 
    $Path="/acme"


— End of Listing —