1 /* httplib.c, v1.0 2008/09/07 9:54 */
4 * Note:(Http increased download a need to amend some of the things)
5 * 1.cons/files: add "file pmon/netio/httplib.c inet"
6 * 2.ib/libc/open.c: add"else if(strpat(dname, "http:*"))" in function of open
7 * 3.pmon/netio/netio.c: add"else {return fd;}" in line 89 of function of netiopen
8 * 4../sys/netinet/in_proto.c: " #if NTCP > 0"->"#if NTCP = 0" in line 137
10 *The first test was a success in zloader.2fdev.cs5536 in 2008/09/07 9:50
13 * ifaddr rtk0 10.0.0.187
14 * load tftp://10.0.0.111/gzram
16 * ifaddr rtk0 10.0.0.187
17 * load http://10.0.0.111/gzram
20 *The last test was a success in zloader.2fdev.cs5536 in 2008/09/07 11:13
24 #include <sys/types.h>
25 #include <sys/param.h>
26 #include <sys/socket.h>
27 #include <sys/ioctl.h>
30 #include <sys/syslog.h>
31 #include <sys/endian.h>
33 #include <netinet/in.h>
49 * sockaddr_in->hostaddr
51 * flags ->limits of authority
55 struct sockaddr_in sin
;
68 * fp_buf ->Guidelines document
69 * buf_size ->size of file
74 static int httpopen (int, struct Url
*, int, int);
75 static int httpread (int, void *, int);
76 static int httpwrite ();
77 static off_t
httplseek (int, long, int );
78 static int httpioctl ();
79 static int httpclose ();
80 static int my_write(char *, int, char *);
81 static int my_read(int, struct httpfile
*);
88 static NetFileOps httpops
= {
97 static void init_netfs
__P((void)) __attribute__ ((constructor
));
102 netfs_init(&httpops
);
106 httpopen (int fd
, struct Url
*url
, int flags
, int perms
)
109 struct httpfile
*http
;
110 NetFile
*nfp
= (NetFile
*)_file
[fd
].data
;
111 char hbuf
[MAXHOSTNAMELEN
];
112 int oflags
= flags
& O_ACCMODE
;
116 if(strlen(url
->hostname
) != 0)
118 host
= url
->hostname
;
122 host
= getenv("httphost");
125 log(LOG_INFO
, "http: missing/bad host name: %s\n", url
->filename
);
126 errno
= EDESTADDRREQ
;
130 //set up struct httpfile*, and clear 0
131 http
= (struct httpfile
*)malloc(sizeof (struct httpfile
));
132 bzero(http
, sizeof(struct httpfile
));
134 nfp
->data
= (void *)http
;
136 //set socket,and get number of socket
137 http
->sock
= socket(AF_INET
, SOCK_STREAM
, 0);
139 //sin_family = AF_INET,and bind
140 http
->sin
.sin_family
= AF_INET
;
141 http
->sin
.sin_port
= htons(url
->port
?url
->port
:80);
145 //get the message of host
146 hp
= gethostbyname(host
);
149 http
->sin
.sin_family
= hp
->h_addrtype
;
150 bcopy(hp
->h_addr
, (void *)&http
->sin
.sin_addr
, hp
->h_length
);
151 strncpy(hbuf
, hp
->h_name
, sizeof(hbuf
)-1);
152 hbuf
[sizeof(hbuf
)-1]='\0';
162 connect_fd
= connect(http
->sock
,&http
->sin
, sizeof(http
->sin
));
168 * my_write ->add head of http,send filename address and port of host
169 * my_read ->del head of http,receive file
172 my_write(host
, http
->sock
, url
->filename
);
173 my_read(http
->sock
, http
);
174 http
->end
=read(http
->sock
, http
->buf
, HPSIZE
);
182 my_write(char *host
, int socketfd
, char *filename
)
184 char html_http
[1000];
186 sprintf(html_http
, "%s%s%s%s%s", "GET /",filename
,
187 " HTTP/1.1\r\nHost:", host
,":8000\r\n"
188 "User-Agent:Mozilla/5.0(X11;U;Linux i686;en-US;rv:1.8.16)Gecko/20061201 Firefox/2.0.0.6(Ubuntu-feisty)\r\n"
189 "Accept:text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
190 "Accept-Language:en-us,en;q=0.5\r\n"
191 "Accept-Encoding:gzip,defalte\r\n"
192 "Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"
194 "Connecting:keep-alive\r\n\r\n");
197 if ((n
= write(socketfd
, html_http
, strlen(html_http
))) == -1)
205 static int sum_number
= 0;
206 int my_readline(int fd
, char *buf
, int count
)
219 sum_number
+= number
;
223 my_read(int socketfd
, struct httpfile
*http
)
225 char rhtml_http
[1000];
231 while((n
= my_readline(socketfd
, rhtml_http
, 1000)) > 2)
233 // write(1, rhtml_http, n);
234 if(strstr(rhtml_http
, "Content-Length") != NULL
)
236 buf_c_l
= rhtml_http
+ 16;
237 strcpy(buf_length
, buf_c_l
);
238 buf_len
= atoi(buf_length
);
241 memset(rhtml_http
, 0 , 1000);
249 * read the buf ->read the file
253 httpread (int fd
,void* buf
,int nread
)
255 struct httpfile
*http
;
260 nfp
= (NetFile
*)_file
[fd
].data
;
261 http
= (struct httpfile
*)nfp
->data
;
262 buf_flag
= http
->buf
;
264 for (nb
= nread
; nb
!= 0 && http
->start
< http
->end
; ) {
266 if (http
->foffs
>= http
->start
&& http
->foffs
< http
->end
) {
267 /* got some data that's in range */
268 n
= http
->end
- http
->foffs
;
270 bcopy(http
->buf
+http
->foffs
-http
->start
, buf
, n
);
275 if (http
->foffs
>= http
->end
) {
276 http
->start
= http
->end
;
277 if(http
->foffs
>=buf_size
)break;
278 n
= read(http
->sock
, http
->buf
, HPSIZE
);
280 http
->end
= http
->start
+ n
;
281 if (http
->flags
& O_NONBLOCK
)
289 httpwrite (fd
, buf
, nwrite
)
303 httplseek (int fd
, long offs
, int how
)
305 struct httpfile
*http
;
308 nfp
= (NetFile
*)_file
[fd
].data
;
309 http
= (struct httpfile
*)nfp
->data
;
327 httpioctl (fd
, op
, argp
)
344 struct httpfile
*http
;
346 nfp
= (NetFile
*)_file
[fd
].data
;
347 http
= (struct httpfile
*)nfp
->data
;