~ubuntu-branches/ubuntu/natty/xfce4-weather-plugin/natty-proposed

« back to all changes in this revision

Viewing changes to panel-plugin/http_client.c

  • Committer: Bazaar Package Importer
  • Author(s): Emanuele Rocca
  • Date: 2004-07-11 12:09:48 UTC
  • Revision ID: james.westby@ubuntu.com-20040711120948-l35brbd021chm9lv
Tags: upstream-0.3.9
ImportĀ upstreamĀ versionĀ 0.3.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "http_client.h"
 
2
#include "debug_print.h"
 
3
 
 
4
int http_connect(gchar *hostname)
 
5
{
 
6
        struct sockaddr_in dest_host;
 
7
        struct hostent *host_address;
 
8
        int fd;
 
9
               
 
10
        if ((host_address = gethostbyname(hostname)) == NULL)
 
11
                return -1;
 
12
 
 
13
        if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
 
14
                return -1;
 
15
       
 
16
        dest_host.sin_family = AF_INET;
 
17
        dest_host.sin_addr = *((struct in_addr *)host_address->h_addr);
 
18
        dest_host.sin_port = htons(80);
 
19
        memset(&(dest_host.sin_zero), '\0', 8);
 
20
 
 
21
        if (connect(fd, (struct sockaddr *)&dest_host, sizeof(struct sockaddr)) == -1)
 
22
                return -1;
 
23
       
 
24
        /* TODO fcntl(fd, F_SETFL, O_NONBLOCK); */
 
25
 
 
26
        return fd;
 
27
}
 
28
 
 
29
gboolean http_send_req(int fd, gchar *url, gchar *hostname)
 
30
{
 
31
        int len_request, n, bytes_sent = 0;
 
32
        gchar *request;
 
33
        gboolean error = FALSE;
 
34
 
 
35
        request = g_strdup_printf("GET %s HTTP/1.0\r\n"
 
36
                                  "Host: %s\r\n\r\n", url, hostname);
 
37
 
 
38
        len_request = strlen(request);      
 
39
 
 
40
 
 
41
         while(bytes_sent < len_request) {
 
42
                 n = send(fd, request + bytes_sent, len_request - bytes_sent, 0);
 
43
 
 
44
                 if (n == -1)
 
45
                 {
 
46
                        DEBUG_PRINT("Error while sending request\n", NULL);
 
47
                         error = TRUE;
 
48
                         break;
 
49
                 }
 
50
 
 
51
                 bytes_sent += n;
 
52
         }
 
53
 
 
54
         g_free(request);
 
55
 
 
56
         return error;
 
57
}
 
58
       
 
59
int http_recv(int fd, gchar **buffer)
 
60
{
 
61
        int n = 0; // 1 = good, 0 = conn terminated, -1 = error
 
62
        gchar thisbuffer[1024]; 
 
63
        
 
64
        n = recv(fd, thisbuffer, 1023, 0);
 
65
 
 
66
        
 
67
 
 
68
        if (n == -1)
 
69
        {
 
70
                *buffer = NULL;
 
71
        }
 
72
        else if (n == 0) {
 
73
                *buffer = NULL;
 
74
        }
 
75
        else {
 
76
                
 
77
                thisbuffer[n] = '\0';
 
78
                *buffer = g_strdup((const gchar *)thisbuffer); 
 
79
        }
 
80
 
 
81
        
 
82
 
 
83
        return n;
 
84
}
 
85
                
 
86
gboolean http_get_header(int fd, gchar **buffer)
 
87
{
 
88
        gchar lastchar = 0, *thisbuffer;
 
89
        int l;
 
90
        
 
91
        while((l = http_recv(fd, &thisbuffer)) > 0)
 
92
        {
 
93
                gboolean found = FALSE;
 
94
                gchar *where;
 
95
                gchar *p;
 
96
 
 
97
                
 
98
                
 
99
                if (lastchar == '\r' &&
 
100
                                (p = g_strstr_len(thisbuffer, 3, "\n\r\n"))) {
 
101
                        
 
102
                        where = p + 3;
 
103
                        found = TRUE;
 
104
                        
 
105
                }
 
106
                else if (p = strstr(thisbuffer, "\r\n\r\n")) {
 
107
                        where = p + 4;
 
108
                        found = TRUE;
 
109
                }
 
110
 
 
111
                if (found)
 
112
                {
 
113
                        //TODO check if at end
 
114
                        *buffer = g_strdup(where);
 
115
                }
 
116
                else
 
117
                        lastchar = thisbuffer[l];
 
118
                
 
119
                g_free(thisbuffer);
 
120
 
 
121
                if (found) 
 
122
                        return TRUE;
 
123
        }
 
124
 
 
125
        return FALSE;
 
126
}
 
127
 
 
128
gboolean http_get(gchar *url, gchar *hostname, gboolean savefile, gchar **fname_buff)
 
129
{
 
130
        int fd, error;
 
131
        FILE *file;
 
132
        gchar *buffer = NULL;
 
133
        gchar *retstr = NULL;
 
134
 
 
135
 
 
136
        if ((fd = http_connect(hostname)) == -1)
 
137
                return FALSE;
 
138
 
 
139
        if (http_send_req(fd, url, hostname) == -1) {
 
140
                return FALSE;
 
141
        }
 
142
 
 
143
        if (savefile)
 
144
        {
 
145
                file = fopen(*fname_buff, "w");
 
146
 
 
147
                if (!file)
 
148
                {
 
149
                       DEBUG_PRINT("Error opening file %s\n", *fname_buff);
 
150
                        return FALSE;
 
151
                }
 
152
        }
 
153
 
 
154
 
 
155
        if (http_get_header(fd, &buffer) == FALSE)
 
156
                return FALSE;
 
157
 
 
158
 
 
159
        if (buffer)
 
160
        {
 
161
                int l = strlen(buffer);
 
162
                
 
163
                if (savefile)
 
164
                        fwrite(buffer, sizeof(char), l, file);
 
165
                else
 
166
                        retstr = g_strdup(buffer);
 
167
 
 
168
                g_free(buffer);
 
169
        }
 
170
 
 
171
        while((error = http_recv(fd, &buffer)) > 0)
 
172
        {
 
173
                
 
174
                if (savefile) 
 
175
                {
 
176
                        int l = strlen(buffer);
 
177
                        fwrite(buffer, sizeof(char), l, file);
 
178
                }
 
179
                else
 
180
                {
 
181
                        gchar *str;
 
182
 
 
183
                        if (retstr) 
 
184
                        {
 
185
                                str = g_strconcat(retstr, buffer, NULL);
 
186
                                g_free(retstr);
 
187
                                retstr = str;
 
188
                        }
 
189
                        else
 
190
                                retstr = g_strdup(str);
 
191
                }
 
192
                        
 
193
 
 
194
                g_free(buffer);
 
195
        }
 
196
 
 
197
        if (error == -1)
 
198
        {
 
199
                fclose(file); //TODO unlink
 
200
                g_free(retstr);
 
201
                return FALSE;
 
202
        }
 
203
 
 
204
        if (savefile)
 
205
                fclose(file);
 
206
        else
 
207
                *fname_buff = retstr;
 
208
 
 
209
        return TRUE;
 
210
}
 
211
 
 
212
        
 
213
 
 
214
gboolean http_get_file(gchar *url, gchar *hostname, gchar *filename)
 
215
{
 
216
        return http_get(url, hostname, TRUE, &filename);
 
217
}
 
218
 
 
219
gchar *http_get_buffer(gchar *url, gchar *hostname)
 
220
{
 
221
        gchar *buffer = NULL;
 
222
        
 
223
        http_get(url, hostname, FALSE, &buffer);
 
224
 
 
225
        return buffer;
 
226
}