Diffstat (limited to 'noncore/todayplugins/stockticker/libstocks/http.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/todayplugins/stockticker/libstocks/http.c | 87 |
1 files changed, 44 insertions, 43 deletions
diff --git a/noncore/todayplugins/stockticker/libstocks/http.c b/noncore/todayplugins/stockticker/libstocks/http.c index 462080d..3078746 100644 --- a/noncore/todayplugins/stockticker/libstocks/http.c +++ b/noncore/todayplugins/stockticker/libstocks/http.c | |||
@@ -1,33 +1,34 @@ | |||
1 | /* libstocks - Library to get current stock quotes from Yahoo Finance | 1 | /* libstocks - Library to get current stock quotes from Yahoo Finance |
2 | * | 2 | * |
3 | * Copyright (C) 2000 Eric Laeuffer | 3 | * Copyright (C) 2000 Eric Laeuffer |
4 | * | 4 | * |
5 | * This library is free software; you can redistribute it and/or | 5 | * This library is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU Library General Public | 6 | * modify it under the terms of the GNU Library General Public |
7 | * License as published by the Free Software Foundation; either | 7 | * License as published by the Free Software Foundation; either |
8 | * version 2 of the License, or (at your option) any later version. | 8 | * version 2 of the License, or (at your option) any later version. |
9 | * | 9 | * |
10 | * This library is distributed in the hope that it will be useful, | 10 | * This library is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | * Library General Public License for more details. | 13 | * Library General Public License for more details. |
14 | * | 14 | * |
15 | * You should have received a copy of the GNU Library General Public | 15 | * You should have received a copy of the GNU Library General Public |
16 | * License along with this library; if not, write to the | 16 | * License along with this library; if not, write to the |
17 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 17 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
18 | * Boston, MA 02111-1307, USA. | 18 | * Boston, MA 02111-1307, USA. |
19 | */ | 19 | */ |
20 | 20 | ||
21 | #define __HTTP_C__ | 21 | #define __HTTP_C__ |
22 | #define __UNIX__ | ||
22 | 23 | ||
23 | #ifdef __UNIX__ | 24 | #ifdef __UNIX__ |
24 | #include <sys/socket.h> | 25 | #include <sys/socket.h> |
25 | #include <netinet/in.h> | 26 | #include <netinet/in.h> |
26 | #include <arpa/inet.h> | 27 | #include <arpa/inet.h> |
27 | #include <netdb.h> | 28 | #include <netdb.h> |
28 | #include <unistd.h> | 29 | #include <unistd.h> |
29 | #elif __WINDOWS__ | 30 | #elif __WINDOWS__ |
30 | #include <winsock.h> | 31 | #include <winsock.h> |
31 | #include <mbstring.h> | 32 | #include <mbstring.h> |
32 | #endif | 33 | #endif |
33 | 34 | ||
@@ -76,27 +77,27 @@ libstocks_return_code http_get(char *http_file, char *http_server, char **pdata) | |||
76 | /* get host info by name :*/ | 77 | /* get host info by name :*/ |
77 | if ((host = gethostbyname( http_proxy_server ? http_proxy_server : http_server))) | 78 | if ((host = gethostbyname( http_proxy_server ? http_proxy_server : http_server))) |
78 | { | 79 | { |
79 | memset((char *) &server,0, sizeof(server)); | 80 | memset((char *) &server,0, sizeof(server)); |
80 | memmove((char *) &server.sin_addr, host->h_addr, host->h_length); | 81 | memmove((char *) &server.sin_addr, host->h_addr, host->h_length); |
81 | server.sin_family = host->h_addrtype; | 82 | server.sin_family = host->h_addrtype; |
82 | server.sin_port = (unsigned short) htons( http_proxy_server ? http_proxy_port : 80 ); | 83 | server.sin_port = (unsigned short) htons( http_proxy_server ? http_proxy_port : 80 ); |
83 | 84 | ||
84 | } else | 85 | } else |
85 | { | 86 | { |
86 | 87 | ||
87 | #ifdef DEBUG | 88 | #ifdef DEBUG |
88 | printf(" gethostbyname : NOK\n"); | 89 | printf(" gethostbyname : NOK\n"); |
89 | #endif | 90 | #endif |
90 | return ERRHOST; | 91 | return ERRHOST; |
91 | } | 92 | } |
92 | 93 | ||
93 | /* create socket */ | 94 | /* create socket */ |
94 | if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) | 95 | if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) |
95 | { | 96 | { |
96 | 97 | ||
97 | #ifdef DEBUG | 98 | #ifdef DEBUG |
98 | printf(" create socket : NOK\n"); | 99 | printf(" create socket : NOK\n"); |
99 | #endif | 100 | #endif |
100 | return ERRSOCK; | 101 | return ERRSOCK; |
101 | } | 102 | } |
102 | 103 | ||
@@ -112,142 +113,142 @@ libstocks_return_code http_get(char *http_file, char *http_server, char **pdata) | |||
112 | #ifdef __UNIX__ | 113 | #ifdef __UNIX__ |
113 | close(s); | 114 | close(s); |
114 | #elif __WINDOWS__ | 115 | #elif __WINDOWS__ |
115 | closesocket(s); | 116 | closesocket(s); |
116 | #endif | 117 | #endif |
117 | return ERRCONN; | 118 | return ERRCONN; |
118 | } | 119 | } |
119 | 120 | ||
120 | /* create header */ | 121 | /* create header */ |
121 | if (http_proxy_server) | 122 | if (http_proxy_server) |
122 | { | 123 | { |
123 | sprintf(header,"GET http://%.128s:80%.256s HTTP/1.0\015\012\015\012", | 124 | sprintf(header,"GET http://%.128s:80%.256s HTTP/1.0\015\012\015\012", |
124 | http_server, http_file); | 125 | http_server, http_file); |
125 | } | 126 | } |
126 | else | 127 | else |
127 | { | 128 | { |
128 | sprintf(header,"GET %s HTTP/1.0\015\012\015\012",http_file); | 129 | sprintf(header,"GET %s HTTP/1.0\015\012\015\012",http_file); |
129 | } | 130 | } |
130 | 131 | ||
131 | hlg=strlen(header); | 132 | hlg=strlen(header); |
132 | 133 | ||
133 | /* send header */ | 134 | /* send header */ |
134 | #ifdef __UNIX__ | 135 | #ifdef __UNIX__ |
135 | if (write(s,header,hlg)!=hlg) | 136 | if (write(s,header,hlg)!=hlg) |
136 | #elif __WINDOWS__ | 137 | #elif __WINDOWS__ |
137 | if (send(s,header,hlg, 0)!=hlg) | 138 | if (send(s,header,hlg, 0)!=hlg) |
138 | #endif | 139 | #endif |
139 | { | 140 | { |
140 | #ifdef DEBUG | 141 | #ifdef DEBUG |
141 | printf(" send header : NOK\n"); | 142 | printf(" send header : NOK\n"); |
142 | #endif | 143 | #endif |
143 | return ERRWHEA; | 144 | return ERRWHEA; |
144 | } | 145 | } |
145 | 146 | ||
146 | data_lgr = 0; | 147 | data_lgr = 0; |
147 | r=1; | 148 | r=1; |
148 | while(r) | 149 | while(r) |
149 | { | 150 | { |
150 | /* Clear Buffer */ | 151 | /* Clear Buffer */ |
151 | memset(buf,0,BUF_SIZE+1); | 152 | memset(buf,0,BUF_SIZE+1); |
152 | 153 | ||
153 | #ifdef __UNIX__ | 154 | #ifdef __UNIX__ |
154 | r=read(s,buf,BUF_SIZE); | 155 | r=read(s,buf,BUF_SIZE); |
155 | #elif __WINDOWS__ | 156 | #elif __WINDOWS__ |
156 | r=recv(s,buf,BUF_SIZE,0); | 157 | r=recv(s,buf,BUF_SIZE,0); |
157 | #endif | 158 | #endif |
158 | 159 | ||
159 | if (r) | 160 | if (r) |
160 | { | 161 | { |
161 | if(!data_lgr) | 162 | if(!data_lgr) |
162 | { | 163 | { |
163 | if((data = malloc(r+1))==NULL) | 164 | if((data = malloc(r+1))==NULL) |
164 | { | 165 | { |
165 | fprintf(stderr,"Memory allocating error (%s line %d)\n" | 166 | fprintf(stderr,"Memory allocating error (%s line %d)\n" |
166 | ,__FILE__, __LINE__); | 167 | ,__FILE__, __LINE__); |
167 | exit(1); | 168 | exit(1); |
168 | } | 169 | } |
169 | 170 | ||
170 | memcpy(data,buf,r); | 171 | memcpy(data,buf,r); |
171 | data_lgr = r; | 172 | data_lgr = r; |
172 | data[r]=0; | 173 | data[r]=0; |
173 | } | 174 | } |
174 | else | 175 | else |
175 | { | 176 | { |
176 | if((temp = malloc(r+data_lgr+1))==NULL) | 177 | if((temp = malloc(r+data_lgr+1))==NULL) |
177 | { | 178 | { |
178 | fprintf(stderr,"Memory allocating error (%s line %d)\n" | 179 | fprintf(stderr,"Memory allocating error (%s line %d)\n" |
179 | ,__FILE__, __LINE__); | 180 | ,__FILE__, __LINE__); |
180 | exit(1); | 181 | exit(1); |
181 | } | 182 | } |
182 | memcpy(temp, data, data_lgr); | 183 | memcpy(temp, data, data_lgr); |
183 | memcpy(temp+data_lgr, buf, r); | 184 | memcpy(temp+data_lgr, buf, r); |
184 | temp[r+data_lgr]=0; | 185 | temp[r+data_lgr]=0; |
185 | data_lgr += r; | 186 | data_lgr += r; |
186 | free(data); | 187 | free(data); |
187 | data = temp; | 188 | data = temp; |
188 | } | 189 | } |
189 | } | 190 | } |
190 | } | 191 | } |
191 | 192 | ||
192 | /* close socket */ | 193 | /* close socket */ |
193 | #ifdef __UNIX__ | 194 | #ifdef __UNIX__ |
194 | close(s); | 195 | close(s); |
195 | #elif __WINDOWS__ | 196 | #elif __WINDOWS__ |
196 | closesocket(s); | 197 | closesocket(s); |
197 | #endif | 198 | #endif |
198 | 199 | ||
199 | #ifdef DEBUG | 200 | #ifdef DEBUG |
200 | printf("%s\n", data); | 201 | printf("%s\n", data); |
201 | #endif | 202 | #endif |
202 | 203 | ||
203 | /* get headers to test status line */ | 204 | /* get headers to test status line */ |
204 | /* and to split headers and content */ | 205 | /* and to split headers and content */ |
205 | 206 | ||
206 | temp = data; | 207 | temp = data; |
207 | header_founded = 0; | 208 | header_founded = 0; |
208 | while( !header_founded ) | 209 | while( !header_founded ) |
209 | { | 210 | { |
210 | if (*temp==0) return ERRRHEA; | 211 | if (*temp==0) return ERRRHEA; |
211 | 212 | ||
212 | if( *temp==0x0A ) | 213 | if( *temp==0x0A ) |
213 | { | 214 | { |
214 | /* test if it is the header end */ | 215 | /* test if it is the header end */ |
215 | temp ++; | 216 | temp ++; |
216 | if (*temp == 0x0D) temp++; | 217 | if (*temp == 0x0D) temp++; |
217 | if (*temp == 0x0A) header_founded = 1; | 218 | if (*temp == 0x0A) header_founded = 1; |
218 | } | 219 | } |
219 | else | 220 | else |
220 | temp++; | 221 | temp++; |
221 | } | 222 | } |
222 | 223 | ||
223 | *temp = 0; | 224 | *temp = 0; |
224 | temp++; | 225 | temp++; |
225 | 226 | ||
226 | sscanf(data,"HTTP/1.%*d %03d",&error_code); | 227 | sscanf(data,"HTTP/1.%*d %03d",&error_code); |
227 | 228 | ||
228 | if (error_code != 200) | 229 | if (error_code != 200) |
229 | { | 230 | { |
230 | #ifdef DEBUG | 231 | #ifdef DEBUG |
231 | printf(" HTTP error code : %d\n", error_code); | 232 | printf(" HTTP error code : %d\n", error_code); |
232 | #endif | 233 | #endif |
233 | free(data); | 234 | free(data); |
234 | return ERRPAHD; | 235 | return ERRPAHD; |
235 | } | 236 | } |
236 | 237 | ||
237 | if ((csv_ptr = malloc(strlen(temp)+1))==NULL) | 238 | if ((csv_ptr = malloc(strlen(temp)+1))==NULL) |
238 | { | 239 | { |
239 | free(data); | 240 | free(data); |
240 | fprintf(stderr,"Memory allocating error (%s line %d)\n" | 241 | fprintf(stderr,"Memory allocating error (%s line %d)\n" |
241 | ,__FILE__, __LINE__); | 242 | ,__FILE__, __LINE__); |
242 | exit(1); | 243 | exit(1); |
243 | } | 244 | } |
244 | 245 | ||
245 | memcpy(csv_ptr, temp, strlen(temp)+1); | 246 | memcpy(csv_ptr, temp, strlen(temp)+1); |
246 | free(data); | 247 | free(data); |
247 | 248 | ||
248 | #ifdef DEBUG | 249 | #ifdef DEBUG |
249 | printf(" CSV\n"); | 250 | printf(" CSV\n"); |
250 | printf("%s,\n", csv_ptr); | 251 | printf("%s,\n", csv_ptr); |
251 | #endif | 252 | #endif |
252 | 253 | ||
253 | *pdata = csv_ptr; | 254 | *pdata = csv_ptr; |