#include "http_lib.h"
int split_url(char* url,char*host,char* path,unsigned short* port)
{
char *p,host_path[URL_LEN];
if(strlen(url)>URL_LEN-1){
fprintf(stderr,"URLが長すぎます.\n"); return 1;
}
if(strstr(url,"http://") && sscanf(url,"http://%s",host_path)){
if((p = strchr(host_path,'/')) != NULL){
strcpy(path,p); *p = '\0'; strcpy(host,host_path);
} else {
strcpy(host,host_path);
}
if((p = strchr(host,':')) != NULL){
*port = atoi(p+1);
*p = '\0';
}
if(*port <= 0) *port = 80;
} else {
fprintf(stderr,"URLは [http://host/path] の形式で指定してください.\n");
return 1;
}
return 0;
}
int http_open(char* host,int port)
{
int s;
struct hostent *servhost;
struct sockaddr_in server;
struct servent *service;
servhost = gethostbyname(host);
if(servhost == NULL){
perror(host); return -1;
}
bzero((char*)&server,sizeof(server));
server.sin_family = AF_INET;
bcopy(servhost->h_addr,(char*)&server.sin_addr,servhost->h_length);
if(port != 0){
server.sin_port = htons(port);
} else {
service = getservbyname("http", "tcp");
if(service != NULL) server.sin_port = service->s_port;
else server.sin_port = htons(80);
}
if((s = socket(AF_INET,SOCK_STREAM,0)) < 0){
perror("socket"); return -1;
}
if(connect(s,(struct sockaddr *)&server,sizeof(server)) == -1){
perror("connect"); return -1;
}
return s;
}
void http_request(int s,char* host,char* path,int port)
{
char send_buf[URL_LEN];
sprintf(send_buf,"GET %s HTTP/1.0\r\n",path);
write(s,send_buf,strlen(send_buf));
sprintf(send_buf,"Host: %s:%d\r\n",host,port);
write(s,send_buf,strlen(send_buf));
sprintf(send_buf,"\r\n");
write(s,send_buf,strlen(send_buf));
}