Hello,

Gianluca Cannata, le dim. 15 nov. 2026 20:52:29 +0100, a ecrit:
> I hope the patch attached to this email does not get mangled.

Yes, it worked.

> diff --git a/Makefile.am b/Makefile.am
> index 27d2588..473f60b 100644
> --- a/Makefile.am
> +++ b/Makefile.am
> @@ -2,7 +2,7 @@
>  
>  bin_PROGRAMS=httpfs
>  
> -HTTPFS_SHARED_LIBS = -lxml2 -lpthread -lnetfs -lfshelp -lports -liohelp 
> -lshouldbeinlibc -lhurdbugaddr
> +HTTPFS_SHARED_LIBS = -lcurl -lxml2 -lpthread -lnetfs -lfshelp -lports 
> -liohelp -lshouldbeinlibc -lhurdbugaddr

This is however not against the latest CVS source.

> diff --git a/http.c b/http.c
> index 50184c0..6bb6c11 100644
> --- a/http.c
> +++ b/http.c
> @@ -34,140 +34,100 @@
>  #include <hurd/hurd_types.h>
>  #include <hurd/netfs.h>
>  
> -/* do a DNS lookup for NAME and store result in *ENT */
> -error_t lookup_host (char *url, struct hostent **ent) 
> +struct curl_read_request {
> +     char *buffer;
> +     loff_t offset;
> +};

Please document what this structure is for and what the fields mean
exactly, notably the role of offset.

You'll also want a size field to know how much data is desired.

> +
> +static size_t curl_write_response_to_buffer(char *data, size_t size, size_t 
> nmemb, void *userdata)
>  {
> -     if ( (*ent = gethostbyname(url)) == NULL )
> -     {
> -             fprintf(stderr,"wrong host name\n");
> -             return EINVAL;
> +     struct curl_read_request *request = (struct curl_read_request *) 
> userdata;
> +     if (!request) {
> +             return 0;
>       }
> -     return 0;
> +
> +     size_t total_size = size * nmemb - request->offset;

It's not that simple. curl_write_response_to_buffer will get called
several times. You need to track down how much data was already
processed by curl_write_response_to_buffer, and when you end up around
request->offset, start copying data with memcpy into request->buffer,
possibly several times up to getting all the data. And once you have it
all, return CURL_WRITEFUNC_ERROR to abort the transfer since we don't
need the subsequent data.

> +     if (total_size == 0) {
> +             return 0;
> +     }
> +
> +     memcpy(request->buffer, data + request->offset, total_size);
> +     
> +     return total_size;
>  }

That is surprising. The curl documentation says that 

“
Your callback should return the number of bytes actually taken care of.
If that amount differs from the amount passed to your callback function,
it signals an error condition to the library
”

Here, if offset is not 0, you are not returning the same amount as
passed to the function.

>  
>  /* open a connection with the remote web server */
> -error_t open_connection(struct netnode *node, int *fd,off_t *head_len) 
> +error_t open_connection(struct netnode *node, char *buffer, loff_t offset, 
> off_t *head_len) 
>  {
>       /* HTTP GET command returns head and body so we have to prune the
>        * head. *HEAD_LEN indicates the header length, for pruning upto that */
> -     
> -     error_t err;
> -     struct hostent *hptr;
> -     struct sockaddr_in dest;
> -     ssize_t written;
> -     size_t towrite;
> -     char buffer[4096];
> -     ssize_t bytes_read;
> -     char *token,*mesg;
> -     int code;
> -     char delimiters0[] = " ";
> -     char delimiters1[] = "\n";
> +     error_t err = 0;
> +     long http_status_code = 200;

The variable does not need to be initialized. It would actually even
misguide the reader.

> +     double http_content_length = 0.0f;
> +     CURLcode curl_err = CURLE_OK;

Same for these.

> +     struct curl_read_request *request;
>  
> -     bzero(&dest,sizeof(dest));
> -     dest.sin_family = AF_INET;
> -     dest.sin_port = htons (port);
> +     request = malloc(sizeof(struct curl_read_request));
> +     memset(request, 0, sizeof(struct curl_read_request));
> +     request->buffer = buffer;
> +     request->offset = offset;
>  
> -     if ( !strcmp(ip_addr,"0.0.0.0") )
> -     {
> -             /* connection is not through a proxy server
> -              * find IP addr. of remote server */
> -             err = lookup_host (node->url, &hptr);
> -             if (err)
> -             {
> -                     fprintf(stderr,"Could not find IP addr %s\n",node->url);
> -                     return err;
> -             }
> -             dest.sin_addr = *(struct in_addr *)hptr->h_addr;
> +     CURL *curl = curl_easy_init();
> +     if (!curl) {
> +             return (err = ENOMEM);

There is no need to set 'err' if you are returning anyway.

>       }
> -     else
> -     {
> -             /* connection is through the proxy server
> -              * need not find IP of remote server
> -              * find IP of the proxy server */
> -             if ( inet_aton(ip_addr,&dest.sin_addr) == 0 )
> -             {
> -                     fprintf(stderr,"Invalid IP for proxy\n");
> -                     return -1;
> -             }
> +
> +     if (!strcmp(ip_addr, "0.0.0.0")) {
> +             /* Connection is not through a proxy server. Do nothing. */
> +     } else {
> +             /* Connection is through a proxy server. */
> +             curl_easy_setopt(curl, CURLOPT_PROXY, ip_addr);
>       }
>  
> -     if (debug_flag)
> -             fprintf (stderr, "trying to open %s:%d/%s\n", node->url,
> -                             port, node->conn_req);
> +     curl_easy_setopt(curl, CURLOPT_URL, node->url);
> +     curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L);
> +     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, 
> curl_write_response_to_buffer);
> +     curl_easy_setopt(curl, CURLOPT_WRITEDATA, request);
> +     curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);

We would also want to set the CURLOPT_FOLLOWLOCATION option.

>  
> -     *fd = socket (AF_INET, SOCK_STREAM, 0);
> -     if (*fd == -1)
> -     {
> -             fprintf(stderr,"Socket creation error\n");
> -             return errno;
> -     }
> +     if (debug_flag)
> +             fprintf (stderr, "trying to open %s:%d\n", node->url,
> +                             port);
>  
> -     err = connect (*fd, (struct sockaddr *)&dest, sizeof (dest));
> -     if (err == -1)
> -     {
> -             fprintf(stderr,"Cannot connect to remote host\n");
> -             return errno;
> +     curl_err = curl_easy_perform(curl);
> +     if (curl_err != CURLE_OK) {
> +             fprintf (stderr, "curl_easy_perform(): %s\n", 
> curl_easy_strerror(curl_err));
> +             err = EAGAIN;
> +             goto cleanup;
>       }
>  
> -     /* Send a HEAD request find header length */
> -     sprintf(buffer,"HEAD %s HTTP/1.0\r\nHost: 
> %s\r\n\r\n",node->conn_req,node->url);
> -     towrite = strlen (buffer);
> -     written = TEMP_FAILURE_RETRY (write (*fd, buffer, towrite));
> -     if ( written == -1 || written < towrite )
> -     {
> -             fprintf(stderr,"Could not send an HTTP request to host\n");
> -             return errno;
> -     }
> -     
> -     bytes_read = read(*fd,buffer,sizeof(buffer));
> -     if ( bytes_read < 0 ) 
> -     {
> -             fprintf(stderr,"Error with HEAD read\n");
> -             return errno;
> +     curl_err = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, 
> &http_status_code);
> +     if (curl_err != CURLE_OK) {
> +             fprintf(stderr, "Cannot get HTTP status code: %s\n", 
> curl_easy_strerror(curl_err));
> +             err = EAGAIN;
> +             goto cleanup;
> +     } else if (http_status_code != 200) {
> +             fprintf(stderr, "HTTP status code is: %ld\n", http_status_code);
> +             err = EIO;
> +             goto cleanup;
>       }
>  
> -     *head_len = bytes_read;
> -     token = strtok(buffer,delimiters0);
> -     token = strtok(NULL,delimiters0);
> -     sscanf(token,"%d",&code);
> -     token = strtok(NULL,delimiters1);
> -     mesg = strdup(token);
> -     if ( code != 200 ) 
> -     {
> -             /* page does not exist */
> -             fprintf(stderr,"Error Page not Accesible\n");
> -             fprintf(stderr,"%d %s\n",code,mesg);
> -             return EBADF;
> -     }
> -     
> -     close(*fd);
> -     
> -     /* Send the GET request for the url */
> -     *fd = socket (AF_INET, SOCK_STREAM, 0);
> -     if (*fd == -1)
> -     {
> -             fprintf(stderr,"Socket creation error\n");
> -             return errno;
> +     curl_err = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, 
> &http_content_length);

The curl documentation says that you're supposed to use a curl_off_t
variable, not a double.

But I believe you don't need that at all, see below.

> +     if (curl_err != CURLE_OK) {
> +             fprintf(stderr, "Could not determine HTTP content length: 
> %s\n", curl_easy_strerror(curl_err));
> +             err = EIO;
> +             goto cleanup;
>       }
>  
> -     err = connect (*fd, (struct sockaddr *)&dest, sizeof (dest));
> -     if (err == -1)
> -     {
> -             fprintf(stderr,"Cannot connect to remote host\n");
> -             return errno;
> -     }
> +     *head_len = (off_t) http_content_length;
>  
> -     towrite = strlen (node->comm_buf);
> +     err = 0;
> +     goto cleanup;
>  
> -     /* guard against EINTR failures */
> -     written = TEMP_FAILURE_RETRY (write (*fd, node->comm_buf, towrite));
> -     written += TEMP_FAILURE_RETRY (write (*fd, "\n\n",  2));
> -     if (written == -1 || written < (towrite+2)) 
> -     {
> -             fprintf(stderr,"Could not send GET request to remote host\n");
> -             return errno;
> -     }
> -     return 0;
> +     cleanup:
> +             free(request);
> +             curl_easy_cleanup(curl);
> +             return err;
>  }
>  
>  /* fetch a directory node from the web server
> diff --git a/httpfs.c b/httpfs.c
> index 2da7f0d..163d091 100644
> --- a/httpfs.c
> +++ b/httpfs.c
> @@ -35,6 +35,7 @@ main (int argc, char **argv)
>    error_t err;
>    mach_port_t bootstrap;
>    char *temp_url, *temp, *run;
> +  int is_https = 0;
>    char type;
>    char *comm_buf; /* XXX: Is an http request limited to 200 bytes? */
>    port = 80;
> @@ -42,6 +43,12 @@ main (int argc, char **argv)
>    mode = 1;                  /* means directory */
>    ip_addr = strdup ("0.0.0.0");
>  
> +  CURLcode curl_err = CURLE_OK;
> +  curl_err = curl_global_init(CURL_GLOBAL_ALL);
> +  if (curl_err != CURLE_OK) {
> +    error (1, 0, "libcurl cannot be initialized.");
> +  }
> +
>    if (debug_flag)
>      fprintf (stderr, "pid %d\n", getpid ());
>    httpfs_parse_args (argc, argv);
> @@ -65,9 +72,17 @@ main (int argc, char **argv)
>    if (! temp_url)
>      error (1, errno, "Cannot duplicate url.");
>  
> -  if (!strncmp (temp_url, "http://";, 7))
> +  if (!strncmp (temp_url, "http://";, 7)) {
>      /* go ahead of http:// if given in url */
>      temp_url = temp_url + 7;
> +    is_https = 0;
> +  } else if (!strncmp (temp_url, "https://";, 8)) {
> +    /* go ahead of https:// if given in url */
> +    temp_url = temp_url + 8;
> +    is_https = 1;
> +    port = 443;
> +  }
> +    
>  
>    if (strchr (temp_url, '/') == NULL)
>      error (1, 0, "Url must have a /, e.g., www.gnu.org/");
> @@ -104,7 +119,12 @@ main (int argc, char **argv)
>        dir_tok[no_of_slashes] = strdup ("index.html");
>      }
>  
> -  strcpy (conn_req, "http://";);
> +  if (is_https) {
> +    strcpy (conn_req, "https://";);
> +  } else {
> +    strcpy (conn_req, "http://";);
> +  }
> +
>    if (temp_url[strlen (temp_url) - 1] == '/')
>      {
>        strcat (conn_req, temp_url);
> @@ -140,20 +160,8 @@ main (int argc, char **argv)
>  
>    /* XXX: why is tmp hardcoded? */
>    httpfs->root = httpfs_make_node (type, url, conn_req, comm_buf, "tmp");
> +  httpfs->root->nn->type = HTTP_DIR_NOT_FILLED;
>    netfs_init ();
> -  /* translator set to a directory */
> -  if (mode)
> -    {
> -      /* fill the directory node with files 
> -       * call parser for that 
> -       * only the current directory is filled
> -       * subdirectories within them are indicated by type
> -       * HTTP_DIR_UNFILLED, and are filled as on demand when an
> -       * ls request comes for them */
> -      err = parse (httpfs->root->nn);
> -      if (err)
> -     error (1, err, "Error in Parsing.");
> -    }
>  
>    if (debug_flag)
>      fprintf (stderr, "entering the main loop\n");
> @@ -165,5 +173,6 @@ main (int argc, char **argv)
>  
>    /* NOT REACHED */
>    free (httpfs);
> +  curl_global_cleanup();
>    return 0;
>  }
> diff --git a/httpfs.h b/httpfs.h
> index 3b54e48..1ab0669 100644
> --- a/httpfs.h
> +++ b/httpfs.h
> @@ -21,6 +21,8 @@
>  #include <sys/stat.h>
>  #include <netdb.h>
>  
> +#include <curl/curl.h>
> +
>  #include <hurd/hurd_types.h>
>  
>  #include "config.h"
> @@ -92,7 +94,7 @@ error_t lookup_host (char *url, struct hostent **ent);
>  
>  /* store the remote socket in *FD and establish the connection and send the 
>   * http GET request */

This comment needs to be updated, since we don't have fd any more.

The name of the function itself should probably be renamed, since we are
not just opening a connection, but also reading the data, and closing
the connection. And you will also want a size_t size parameter.

And you probably don't need the head_len parameter any more since curl
will give you the data, and not the header, so netfs_attempt_read won't
have to skip the header.

> -error_t open_connection(struct netnode *node, int *fd,off_t *head_len);
> +error_t open_connection(struct netnode *node, char *buffer, loff_t offset, 
> off_t *head_len);
>  
>  /* make an instance of `struct netnode' with the specified parameters,
>   * return NULL on error */
> diff --git a/netfs.c b/netfs.c
> index 0b49d93..e0e6b4b 100644
> --- a/netfs.c
> +++ b/netfs.c
> @@ -464,18 +464,16 @@ error_t netfs_attempt_readlink (struct iouser *user, 
> struct node *node,
>  error_t netfs_attempt_read (struct iouser *cred, struct node *node,
>                       loff_t offset, size_t *len, void *data)
>  {
> -     error_t err;
> -     static int remote_fd;
> +     error_t err = 0;
>       char buffer[4096];
>       int bytes_read;
>       off_t head_len;
>  
>       if ( offset==0) {
> -     err = open_connection (node->nn, &remote_fd,&head_len);
> +     err = open_connection (node->nn, buffer, offset, &head_len);
>       if (err)
>               return err;
> -     bytes_read = pread(remote_fd, buffer, sizeof(buffer), offset);
> -     memcpy(data,(void 
> *)(buffer+head_len),sizeof(buffer)-(sizeof(char)*head_len));
> +     bytes_read = strlen(buffer) + head_len;

You cannot assume that the data will contain a trailing '\0'. Just make
open_connection return the amount of data actually read, just like the
pread() function.

>       if (bytes_read < 0)
>               err = errno;
>       else {
> @@ -486,8 +484,7 @@ error_t netfs_attempt_read (struct iouser *cred, struct 
> node *node,
>       }
>  
>       else {
> -             bzero(buffer,sizeof(buffer));
> -             bytes_read = pread(remote_fd, data, sizeof(buffer), offset);
> +             bytes_read = strlen(buffer) + head_len;

Ouch, I see that the old code assumes that the reads are consecutive,
that's deemed to just fail completely...

I would say for now just drop the if ( offset==0) condition, and make
open_connection take into account offset. You can have a look at keeping
the connection open (to avoid re-negociation on each read call()) for
later (also along more efficiently supporting offsets, which is possible
with recent versions of http).

>               if (bytes_read < 0)
>                       err = errno;
>               else {
> diff --git a/parsehtml.c b/parsehtml.c
> index 3bd202f..5f3ede0 100644
> --- a/parsehtml.c
> +++ b/parsehtml.c
> @@ -98,7 +98,6 @@ error_t parse(struct netnode *node)
>       /* SAX HTML Parsing Variables */
>       htmlSAXHandlerPtr emptysax = &empty;
>       error_t err;
> -     static int remote_fd;
>       off_t head_len=0,offset=0;
>       char buffer[MAXBUF];
>       int bytes_read=0;
> @@ -125,20 +124,16 @@ error_t parse(struct netnode *node)
>  
>       /* get the HTML stream corresponding to the directory
>        * send a GET request */
> -     err = open_connection(node,&remote_fd,&head_len);
> +     err = open_connection(node, buffer, offset, &head_len);
>       if ( err )
>               return err;
> -     bytes_read = pread(remote_fd,buffer,sizeof(char)*6,offset);
> +     bytes_read = strlen(buffer) + head_len;

Again, just use the size returned by open_connection.

>  
>       if ( bytes_read > 0 ) 
>       {
>               /* Parser operates in Push mode */
>               ctxt = 
> htmlCreatePushParserCtxt(emptysax,NULL,buffer,bytes_read,"abracabadra",0); 
> //file name is irrelevant abracabadra
> -             
> -             while ( 
> (bytes_read=pread(remote_fd,buffer,sizeof(buffer),offset)) > 0)         {
> -                     htmlParseChunk(ctxt,buffer,bytes_read,0);
> -             }
> -             htmlParseChunk(ctxt,buffer,0,1);
> +             htmlParseChunk(ctxt, buffer, bytes_read, 0);
>               doc = ctxt->myDoc;
>               htmlFreeParserCtxt(ctxt);
>               if ( doc != NULL )

Samuel

Reply via email to