http basic authentication

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

http basic authentication

Bastiaan Schaap

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

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

   // Turn on TRACE so we can see what is going on

   // On windows we must always set up the eventloop
     // Register the default set of transport protocols

   // Register the default set of protocol modules

   // Register the default set of BEFORE and AFTER callback functions

   // Register the default set of converters

   // Register the default set of transfer encoders and decoders

   // Register the default set of MIME header parsers

   // 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
     if (url) {
       char *cwd = HTGetCurrentDirectoryURL();
       char *absolute_url = HTParse(url, cwd, PARSE_ALL);
       HTAnchor *anchor = HTAnchor_findAddress(absolute_url);
       chunk = HTLoadAnchorToChunk(anchor, request);

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

           // Debug: print chunk to screen
           //HTPrint("%s", string ? string : "no text");
           // 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) {

   // Clean up the request

   // On windows, shut down eventloop as well

   // Terminate the Library
   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!

Bastiaan Schaap