diff options
author | Martin Peach <mrpeach@users.sourceforge.net> | 2011-01-13 21:15:11 +0000 |
---|---|---|
committer | Martin Peach <mrpeach@users.sourceforge.net> | 2011-01-13 21:15:11 +0000 |
commit | 1c0c6f798f56c25c1677590c4fdc833c2d7a7d0b (patch) | |
tree | bca045203d276598758bdee2ca4c4b49328e6a86 /net/httpreq.c | |
parent | 0c53f952b3eb3dbde73fb1a92dc9d46e041ab6a8 (diff) |
Added a HEAD method.
svn path=/trunk/externals/mrpeach/; revision=14737
Diffstat (limited to 'net/httpreq.c')
-rw-r--r-- | net/httpreq.c | 83 |
1 files changed, 76 insertions, 7 deletions
diff --git a/net/httpreq.c b/net/httpreq.c index e0b764a..bf0b52a 100644 --- a/net/httpreq.c +++ b/net/httpreq.c @@ -11,12 +11,19 @@ static t_class *httpreq_class; typedef struct _httpreq { t_object x_obj; - t_outlet *x_reqout; + t_outlet *x_reqout; + int x_verbosity; } t_httpreq; #define MAX_GETSTRING 256 +static void httpreq_bang(t_httpreq *x); +static void httpreq_get(t_httpreq *x, t_symbol *s); +static void httpreq_head(t_httpreq *x, t_symbol *s); +static void httpreq_verbosity(t_httpreq *x, t_floatarg verbosity); +static void *httpreq_new (void); +void httpreq_setup(void); -void httpreq_bang(t_httpreq *x) +static void httpreq_bang(t_httpreq *x) { post("httpreq_bang %p", x); } @@ -36,7 +43,7 @@ static void httpreq_get(t_httpreq *x, t_symbol *s) } for (i = 0; i < strlen(s->s_name); ++i) buf[i] = s->s_name[i]; buf[i] = 0; -// post("httpreq_get %s (%s)", s->s_name, buf); + if (0 != strncmp("http://", buf, 7)) { pd_error(x, "httpreq_get: url doesn't begin with 'http://' (%d)", len); @@ -77,17 +84,77 @@ Request-Line = Method SP Request-URI SP HTTP-Version CRLF request_line[j++] = 0xA; // <LF> request_line[j] = 0; // terminate string -// output the request line as a list of floats +/* output the request line as a list of floats */ for (i = 0; i < j; ++i) { SETFLOAT(&request_atoms[i], request_line[i]); -// post("%f", request_atoms[i].a_w.w_float); } - post ("j is %d", j); - post("httpreq_get: %s", request_line); + if (x->x_verbosity) post("httpreq_get: %s", request_line); outlet_list(x->x_reqout, &s_list, j, &request_atoms[0]); } +static void httpreq_head(t_httpreq *x, t_symbol *s) +{ /* this is the same as get except for the method */ + unsigned int i, j, len; + char buf[MAX_GETSTRING]; + char request_line[1024]; + t_atom request_atoms[1024]; + + len = strlen (s->s_name); + if (len > MAX_GETSTRING) + { + pd_error(x, "httpreq_head: string too long (%d), should be less than %d", len, MAX_GETSTRING); + return; + } + for (i = 0; i < strlen(s->s_name); ++i) buf[i] = s->s_name[i]; + buf[i] = 0; + + if (0 != strncmp("http://", buf, 7)) + { + pd_error(x, "httpreq_head: url doesn't begin with 'http://' (%d)", len); + return; + } + + j = sprintf(request_line, "HEAD "); + for (i = 7; i < len; ++i) + { /* skip "http://" and the host name */ + if ('/' == buf[i]) break; + } + for (; i < len; ++i, ++j) + { + if (buf[i] <= 0x20) break; + request_line[j] = buf[i]; + } + j += sprintf(&request_line[j], " HTTP/1.1"); + request_line[j++] = 0xD; // <CR> + request_line[j++] = 0xA; // <LF> + j += sprintf(&request_line[j], "Host: "); + for (i = 7; i < len; ++i, ++j) + { /* copy the host name */ + if ('/' == buf[i]) break; + request_line[j] = buf[i]; + } + request_line[j++] = 0xD; // <CR> + request_line[j++] = 0xA; // <LF> + request_line[j++] = 0xD; // <CR> + request_line[j++] = 0xA; // <LF> + request_line[j] = 0; // terminate string + +/* output the request line as a list of floats */ + for (i = 0; i < j; ++i) + { + SETFLOAT(&request_atoms[i], request_line[i]); + } + if (x->x_verbosity) post("httpreq_head: %s", request_line); + outlet_list(x->x_reqout, &s_list, j, &request_atoms[0]); +} + +static void httpreq_verbosity(t_httpreq *x, t_float verbosity) +{ + x->x_verbosity = verbosity; + if (x->x_verbosity != 0) post ("httpreq_verbosity %d", x->x_verbosity); +} + static void *httpreq_new (void) { t_httpreq *x = (t_httpreq *)pd_new(httpreq_class); @@ -103,5 +170,7 @@ void httpreq_setup(void) httpreq_class = class_new(gensym("httpreq"), (t_newmethod)httpreq_new, 0, sizeof(t_httpreq), CLASS_DEFAULT, 0); class_addbang(httpreq_class, httpreq_bang); class_addmethod (httpreq_class, (t_method)httpreq_get, gensym ("GET"), A_DEFSYM, 0); + class_addmethod (httpreq_class, (t_method)httpreq_head, gensym ("HEAD"), A_DEFSYM, 0); + class_addmethod(httpreq_class, (t_method)httpreq_verbosity, gensym("verbosity"), A_FLOAT, 0); } /* fin httpreq.c */ |