1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
/* httptreq.c Started by Martin Peach 20110111 */
/* httpreq will generate http 1.1 requests as lists of bytes suitable for input to tcpclient */
/* See http://www.w3.org/Protocols/rfc2616/rfc2616.html */
#include "m_pd.h"
#include <stdio.h>
#include <string.h>
static t_class *httpreq_class;
typedef struct _httpreq
{
t_object x_obj;
t_outlet *x_reqout;
} t_httpreq;
#define MAX_GETSTRING 256
void httpreq_bang(t_httpreq *x)
{
post("httpreq_bang %p", x);
}
static void httpreq_get(t_httpreq *x, t_symbol *s)
{
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_get: 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;
// 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);
return;
}
/*
5.1 Request-Line
The Request-Line begins with a method token,
followed by the Request-URI and the protocol version,
and ending with CRLF.
The elements are separated by SP characters.
No CR or LF is allowed except in the final CRLF sequence.
Request-Line = Method SP Request-URI SP HTTP-Version CRLF
*/
j = sprintf(request_line, "GET ");
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]);
// post("%f", request_atoms[i].a_w.w_float);
}
post ("j is %d", j);
post("httpreq_get: %s", request_line);
outlet_list(x->x_reqout, &s_list, j, &request_atoms[0]);
}
static void *httpreq_new (void)
{
t_httpreq *x = (t_httpreq *)pd_new(httpreq_class);
if (NULL != x)
{
x->x_reqout = outlet_new(&x->x_obj, &s_anything);
}
return (void *)x;
}
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);
}
/* fin httpreq.c */
|