W3C home > Mailing lists > Public > www-lib@w3.org > October to December 2005

http basic authentication

From: Bastiaan Schaap <b.schaap@siteminds.nl>
Date: Thu, 08 Dec 2005 15:55:56 +0100
Message-ID: <4398497C.60807@siteminds.nl>
To: www-lib@w3.org

Hi all,

It's been quite a while since I did C/C++ programming, and I am trying 
to write a simple program that downloads a text-file from a url, 
protected with basic http authentication. All it needs to do is put the 
result in a text-file called: http_buffer.txt. However, my function 
seems to 'hang' if I try to download from a protected url. I've searched 
the archives, but I could only find one relevant entry which solution I 
have tried.

I have the following function, based on the libwww sample code:

int download(char *url) {
   // A file to save the result in
   FILE *logfile;
  
   // HTTP request and reply variables
   HTRequest *request = HTRequest_new();
   HTList *converters = HTList_new();        /* List of converters */
   HTList *encodings = HTList_new();        /* List of encoders */
   HTChunk *chunk = NULL;
  
   // Initialize libwww core
   HTLibInit("JustATest", "0.1B");

   // set up our own traces
   HTPrint_setCallback(printer);
   HTTrace_setCallback(tracer);

   // Add our own code to handle username/password authentication
   HTAlert_deleteOpcode(HT_A_USER_PW);
   HTAlert_add(httpbasicauth, HT_A_USER_PW);

   // Turn on TRACE so we can see what is going on
   HTSetTraceMessageMask("sop");

   // On windows we must always set up the eventloop
   HTEventInit();
     // Register the default set of transport protocols
   HTTransportInit();

   // Register the default set of protocol modules
   HTProtocolInit();

   // Register the default set of BEFORE and AFTER callback functions
   HTNetInit();

   // Register the default set of converters
   HTConverterInit(converters);
   HTFormat_setConversion(converters);

   // Register the default set of transfer encoders and decoders
   HTTransferEncoderInit(encodings);
   HTFormat_setTransferCoding(encodings);

   // Register the default set of MIME header parsers
   HTMIMEInit();

   // Add our own filter to handle termination
   HTNet_addAfter(term_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);

   // Set up the request and pass it to the Library
   HTRequest_setOutputFormat(request, WWW_SOURCE);
   HTRequest_setPreemptive(request, YES);
  
   // Open the file to store the download in
   logfile=fopen("http_buffer.txt","w");
     if (url) {
       char *cwd = HTGetCurrentDirectoryURL();
       char *absolute_url = HTParse(url, cwd, PARSE_ALL);
       HTAnchor *anchor = HTAnchor_findAddress(absolute_url);
       chunk = HTLoadAnchorToChunk(anchor, request);
       HT_FREE(absolute_url);
       HT_FREE(cwd);

       // If chunk != NULL then we have the data
       if (chunk) {
           char *string;
           // wait until the request is over
           HTEventList_loop(request);
           string = HTChunk_toCString(chunk);
           // Append the string to the result-file
           if(logfile != 0) {
               // Append the line to the logfile
               fprintf(logfile,"%s",string);
           }

           // Debug: print chunk to screen
           //HTPrint("%s", string ? string : "no text");
          
           // Free string
           HT_FREE(string);
       }
   } else {
       // We didn't get a correct url..
       writelogline("Error: no url found for downloading.\n");
   }

   // Close the result-file
   if (logfile != 0) {
       fclose(logfile);
   }

   // Clean up the request
   HTRequest_delete(request);
   HTFormat_deleteAll();

   // On windows, shut down eventloop as well
   HTEventTerminate();

   // Terminate the Library
   HTLibTerminate();
   return 0;
}

If I understand correctly, by calling the HTAlert_add, I've added my own 
'event-handler' for the HT_A_USER_PW event. I've implemented this as 
follows (taken from the CommandLine example program by mr. Nielsen):

PRIVATE BOOL httpbasicauth( HTRequest * request, HTAlertOpcode op,  int 
msgnum, const char * dfault, void * input, HTAlertPar * reply) {
  // Reply the username and password to the server
  writelogline("Sending basic http authentication info...");
  HTAlert_setReplyMessage(reply, "it_is_i");
  HTAlert_setReplySecret(reply, "leclerc");
  return YES;
}

However, if I call my download function with a protected url, it seems 
to 'hang'. From my log I can see it never enters the httpbasicauth 
function... I'm probably making a n00b mistake here, but any help is 
much appreciated!

Cheers,
Bastiaan Schaap
Received on Friday, 9 December 2005 09:11:14 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Monday, 23 April 2007 18:18:45 GMT