Hi Samuel,
This is the first patch that strives to handle HTTP status codes other
than 200 OK.
Only 4xx and 5xx are actually handled in their own way.
Let me know what you think.
Sincerely,
Gianluca
Index: httpfs/http.c
===================================================================
--- httpfs.orig/http.c
+++ httpfs/http.c
@@ -100,6 +100,37 @@ error_t lookup_host (const char *host, s
return 0;
}
+/* read the first line from the socket and return an error_t error */
+static error_t translate_http_status (int fd, ssize_t *nread)
+{
+ char buf[32];
+
+ *nread = read (fd, buf, sizeof (buf) - 1);
+ if (*nread < 12) return EIO;
+ buf[*nread] = '\0';
+
+ if (strncmp (buf, "HTTP/", 5) != 0)
+ return EPROTO;
+
+ int status = atoi (buf + 9);
+
+ if (debug_flag)
+ fprintf (stderr, "HTTP Status: %d\n", status);
+
+ switch (status)
+ {
+ case 200: return 0;
+ case 301:
+ case 302: return EAGAIN;
+ case 401:
+ case 403: return EACCES;
+ case 404: return ENOENT;
+ case 410: return ENOENT;
+ default:
+ return (status >= 500) ? EIO : EINVAL;
+ }
+}
+
/* open a connection with the remote web server */
error_t open_connection(struct netnode *node, int *fd,off_t *head_len)
{
@@ -115,10 +146,6 @@ error_t open_connection(struct netnode *
size_t towrite;
char buffer[4096];
ssize_t bytes_read;
- char *token,*mesg;
- int code;
- char delimiters0[] = " ";
- char delimiters1[] = "\n";
/* 1. Target selection.
* If ip_addr (proxy global variable) is set, we use it.
@@ -165,27 +192,24 @@ error_t open_connection(struct netnode *
fprintf(stderr,"Could not send an HTTP request to host\n");
return errno;
}
+
+ /* Check HTTP status code and handle other than 200 OK only */
+ if ((err = translate_http_status (*fd, &bytes_read)) != 0)
+ {
+ close (*fd);
+ return err;
+ }
- bytes_read = read(*fd,buffer,sizeof(buffer));
- if ( bytes_read < 0 )
+ int n = read(*fd,buffer,sizeof(buffer));
+ if ( n < 0 )
{
- fprintf(stderr,"Error with HEAD read\n");
+ perror ("Failed to read HEAD response");
+ close (*fd);
return errno;
}
+ buffer[n] = '\0';
- *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;
- }
+ *head_len = bytes_read + n;
close(*fd);