提交 bc2283be 编写于 作者: B bernard.xiong

update http client

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@81 bbd45198-f89e-11dd-88c7-29a3b14d5316
上级 d5552523
...@@ -119,99 +119,63 @@ int http_read_line( int socket, char * buffer, int size ) ...@@ -119,99 +119,63 @@ int http_read_line( int socket, char * buffer, int size )
// //
const char *http_resolve_address( struct sockaddr_in *server, const char * url, char *host_addr) const char *http_resolve_address( struct sockaddr_in *server, const char * url, char *host_addr)
{ {
unsigned char w,x,y,z; char *ptr;
const char *char_ptr;
char addr[128];
char port[6] = "80"; /* default port of 80(HTTP) */ char port[6] = "80"; /* default port of 80(HTTP) */
int i = 0, rv; int i = 0, rv;
int is_domain = 0;
/* strip http: */ /* strip http: */
char_ptr = strchr(url, ':'); ptr = strchr(url, ':');
if (char_ptr != NULL) if (ptr != NULL)
{ {
url = char_ptr + 1; url = ptr + 1;
} }
// URL must start with double forward slashes. /* URL must start with double forward slashes. */
if((url[0] != '/') || (url[1] != '/' )) return(NULL); if((url[0] != '/') || (url[1] != '/' )) return(NULL);
url += 2; url += 2;
for(i = 0; ((url[i] != '\0') && (url[i] != '/')) && (i < 127); i++) for(i = 0; ((url[i] != '\0') && (url[i] != '/')) && (i < 30); i++)
{ {
if((((addr[i] = url[i]) < '0') || (url[i] > '9')) && (url[i] != '.')) if((((host_addr[i] = url[i]) < '0') || (url[i] > '9')) && (url[i] != '.'))
{ {
/* get host addr ok. */
host_addr[i] = '\0';
if(url[i] == ':') if(url[i] == ':')
{// allow specification of port in URL like http://www.server.net:8080/ {
unsigned char w;
/* allow specification of port in URL like http://www.server.net:8080/ */
for(w = 0; ((w + i + 1) < 127) && (w < 5) && (url[w + i + 1] != '/') && (url[w + i + 1] != '\0'); w++) for(w = 0; ((w + i + 1) < 127) && (w < 5) && (url[w + i + 1] != '/') && (url[w + i + 1] != '\0'); w++)
port[w] = url[w + i + 1]; port[w] = url[w + i + 1];
port[w] = '\0'; port[w] = '\0';
inet_aton(host_addr, (struct in_addr*)&(server->sin_addr));
/* set the port */
server->sin_port = htons((int) strtol(port, NULL, 10));
rt_kprintf("HTTP: using port %s for connection\n", port); rt_kprintf("HTTP: using port %s for connection\n", port);
break; break;
} }
else // it's a domain name if a non-numeric char is contained in the "server" part of the URL. else
is_domain = 1;
}
}
addr[i] = '\0'; // overwrite last char copied(should be '/', '\0' or ':') with a '\0'
strcpy(host_addr, addr);
if(is_domain)
{ {
// resolve the host name. /* set the port */
rv = dns_gethostbyname(addr, &server->sin_addr, RT_NULL, RT_NULL); server->sin_port = htons((int) strtol(port, NULL, 10));
/* resolve the host name. */
rv = dns_gethostbyname(host_addr, &server->sin_addr, RT_NULL, RT_NULL);
if(rv != 0) if(rv != 0)
{ {
rt_kprintf("HTTP: failed to resolve domain '%s'\n", addr); rt_kprintf("HTTP: failed to resolve domain '%s'\n", host_addr);
return RT_NULL; return RT_NULL;
} }
} }
else
{
// turn '.' characters in ip string into null characters
for(i = 0, w = 0; i < 16; i++)
{
if(addr[i] == '.')
{
addr[i] = '\0';
w++;
} }
} }
if(w < 4)
{ // w is used as a simple error check here
rt_kprintf("HTTP: invalid IP address '%s'\n", addr);
return RT_NULL;
}
i = 0;
// Extract individual ip number octets from string
w = (int)strtol(&addr[i],NULL, 10);
i += (strlen(&addr[i]) + 1);
x = (int)strtol(&addr[i],NULL, 10);
i += (strlen(&addr[i]) + 1);
y = (int)strtol(&addr[i],NULL, 10);
i += (strlen(&addr[i]) + 1);
z = (int)strtol(&addr[i],NULL, 10);
i += (strlen(&addr[i]) + 1);
IP4_ADDR( (struct ip_addr *)&(server->sin_addr) ,w,x,y,z );
}
i = (int) strtol(port, NULL, 10); // set the port
server->sin_port = htons(i);
server->sin_family = AF_INET; server->sin_family = AF_INET;
char_ptr = url; while (*url != '/') url ++;
while(*char_ptr != '/') char_ptr++; return url;
return char_ptr;
} }
// //
...@@ -259,8 +223,7 @@ static int http_connect(struct http_session* session, ...@@ -259,8 +223,7 @@ static int http_connect(struct http_session* session,
// read a line from the header information. // read a line from the header information.
rc = http_read_line( peer_handle, mimeBuffer, 100 ); rc = http_read_line( peer_handle, mimeBuffer, 100 );
rt_kprintf(">> %s\n", mimeBuffer);
rt_kprintf(">> %s", mimeBuffer);
if ( rc < 0 ) return rc; if ( rc < 0 ) return rc;
...@@ -299,7 +262,7 @@ struct http_session* http_session_open(char* url) ...@@ -299,7 +262,7 @@ struct http_session* http_session_open(char* url)
int peer_handle = 0; int peer_handle = 0;
struct sockaddr_in server; struct sockaddr_in server;
const char *get_name; const char *get_name;
char host_addr[100]; char host_addr[32];
struct http_session* session; struct http_session* session;
session = (struct http_session*) rt_malloc(sizeof(struct http_session*)); session = (struct http_session*) rt_malloc(sizeof(struct http_session*));
...@@ -309,7 +272,8 @@ struct http_session* http_session_open(char* url) ...@@ -309,7 +272,8 @@ struct http_session* http_session_open(char* url)
session->position = 0; session->position = 0;
/* Check valid IP address and URL */ /* Check valid IP address and URL */
if((get_name = http_resolve_address(&server, url, host_addr)) == NULL) get_name = http_resolve_address(&server, url, &host_addr[0]);
if(get_name == NULL)
{ {
rt_free(session); rt_free(session);
return RT_NULL; return RT_NULL;
...@@ -383,7 +347,7 @@ int http_session_close(struct http_session* session) ...@@ -383,7 +347,7 @@ int http_session_close(struct http_session* session)
void http_test(char* url) void http_test(char* url)
{ {
struct http_session* session; struct http_session* session;
rt_uint8_t buffer[80]; char buffer[80];
rt_size_t length; rt_size_t length;
session = http_session_open(url); session = http_session_open(url);
...@@ -398,7 +362,7 @@ void http_test(char* url) ...@@ -398,7 +362,7 @@ void http_test(char* url)
rt_memset(buffer, 0, sizeof(buffer)); rt_memset(buffer, 0, sizeof(buffer));
length = http_session_read(session, buffer, sizeof(buffer)); length = http_session_read(session, buffer, sizeof(buffer));
rt_kprintf(buffer); rt_kprintf(buffer);rt_kprintf("\n");
} while (length > 0); } while (length > 0);
http_session_close(session); http_session_close(session);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册