1st cut as BSD-izing uIP web client logic

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@1642 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo
2009-03-26 01:31:43 +00:00
parent 440a3291ff
commit 87eb68e5fd
5 changed files with 390 additions and 557 deletions
+2 -2
View File
@@ -250,8 +250,8 @@ examples/uip
^^^^^^^^^^^^ ^^^^^^^^^^^^
This is a port of uIP example application. It includes conditionally This is a port of uIP example application. It includes conditionally
compiled logic to exercise the uIP webserver, webclient, telnet, smtp, compiled logic to exercise the uIP webserver, telnet, smtp, dhcpc,
dncpc, and resolver. and resolver.
Other configuratin items apply also to the selected network utility. Other configuratin items apply also to the selected network utility.
For example, the additional relevant settings for the uIP webserver For example, the additional relevant settings for the uIP webserver
+1 -37
View File
@@ -75,9 +75,6 @@
# include <net/uip/telnetd.h> # include <net/uip/telnetd.h>
#elif defined(CONFIG_EXAMPLE_UIP_WEBSERVER) #elif defined(CONFIG_EXAMPLE_UIP_WEBSERVER)
# include <net/uip/httpd.h> # include <net/uip/httpd.h>
#elif defined(CONFIG_EXAMPLE_UIP_WEBCLIENT)
# include <net/uip/resolv.h>
# include <net/uip/webclient.h>
#elif !defined(CONFIG_EXAMPLE_UIP_DHCPC) #elif !defined(CONFIG_EXAMPLE_UIP_DHCPC)
# error "No network application specified" # error "No network application specified"
#endif #endif
@@ -174,7 +171,7 @@ int user_start(int argc, char *argv[])
addr.s_addr = HTONL(CONFIG_EXAMPLE_UIP_NETMASK); addr.s_addr = HTONL(CONFIG_EXAMPLE_UIP_NETMASK);
uip_setnetmask("eth0", &addr); uip_setnetmask("eth0", &addr);
#if defined(CONFIG_EXAMPLE_UIP_DHCPC) || defined(CONFIG_EXAMPLE_UIP_WEBCLIENT) #if defined(CONFIG_EXAMPLE_UIP_DHCPC)
/* Set up the resolver */ /* Set up the resolver */
resolv_init(); resolv_init();
@@ -231,12 +228,6 @@ int user_start(int argc, char *argv[])
g_msg_body, strlen(g_msg_body)); g_msg_body, strlen(g_msg_body));
smtp_close(handle); smtp_close(handle);
} }
#elif defined(CONFIG_EXAMPLE_UIP_WEBCLIENT)
printf("Getting webpage\n");
webclient_init();
addr.s_addr = HTONL(CONFIG_EXAMPLE_UIP_DNSADDR);
resolv_conf(&addr);
resolv_query(CONFIG_EXAMPLE_UIP_SERVERURL);
#endif #endif
while(1) while(1)
@@ -249,30 +240,3 @@ int user_start(int argc, char *argv[])
} }
return 0; return 0;
} }
#if defined(CONFIG_EXAMPLE_UIP_WEBCLIENT)
void webclient_closed(void)
{
printf("Webclient: connection closed\n");
}
void webclient_aborted(void)
{
printf("Webclient: connection aborted\n");
}
void webclient_timedout(void)
{
printf("Webclient: connection timed out\n");
}
void webclient_connected(void)
{
printf("Webclient: connected, waiting for data...\n");
}
void webclient_datahandler(char *data, uint16 len)
{
printf("Webclient: got %d bytes of data.\n", len);
}
#endif
+147
View File
@@ -0,0 +1,147 @@
/****************************************************************************
* include/net/uip/webclient.h
* Header file for the HTTP client
*
* Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Based remotely on the uIP webclient which also has a BSD style license:
*
* Author: Adam Dunkels <adam@dunkels.com>
* Copyright (c) 2002, Adam Dunkels.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#ifndef __NET_UIP_WEBCLIENT_H
#define __NET_UIP_WEBCLIENT_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
/****************************************************************************
* Pre-processor definitions
****************************************************************************/
#ifndef CONFIG_NETUTILS_WEBCLIENT_MAXURLLEN
# define CONFIG_NETUTILS_WEBCLIENT_MAXURLLEN 100
#endif
#ifndef CONFIG_NETUTILS_WEBCLIENT_MAXHTTPLINE
# define CONFIG_NETUTILS_WEBCLIENT_MAXHTTPLINE 200
#endif
#ifndef CONFIG_NETUTILS_WEBCLIENT_MAXMIMESIZE
# define CONFIG_NETUTILS_WEBCLIENT_MAXMIMESIZE 32
#endif
#ifndef CONFIG_NETUTILS_WEBCLIENT_MAXHOSTNAME
# define CONFIG_NETUTILS_WEBCLIENT_MAXHOSTNAME 40
#endif
/****************************************************************************
* Public types
****************************************************************************/
/* wget calls a user provided function of the follwoing type to process
* each received chuck of the incoming file data. If the system has a file
* system, then it may just write the data to a file. Or it may buffer the
* file in memory. To facilitate this latter case, the caller may modify
* the buffer address in this callback by writing to buffer and buflen. This
* may be used, for example, to implement double buffering.
*
* Input Parameters:
* buffer - A pointer to a pointer to a buffer. If the callee wishes to
* change the buffer address, it may do so in the callback by writing
* to buffer.
* offset - Offset to the beginning of valid data in the buffer. Offset
* is used to skip over any HTTP header info that may be at the
* beginning of the buffer.
* datend - The end+1 offset of valid data in the buffer. The total number
* of valid bytes is datend - offset.
* buflen - A pointer to the length of the buffer. If the callee wishes
* to change the size of the buffer, it may write to buflen.
*/
typedef void (*wget_callback_t)(FAR char **buffer, int offset,
int datend, FAR int *buflen);
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C" {
#else
#define EXTERN extern
#endif
/****************************************************************************
* Name: wget
*
* Description:
* Obtain the requested file from an HTTP server using the GET method.
*
* Note: If the function is passed a host name, it must already be in
* the resolver cache in order for the function to connect to the web
* server. It is therefore up to the calling module to implement the
* resolver calls and the signal handler used for reporting a resolv
* query answer.
*
* Input Parameters
* host - A pointer to a string containing either a host name or
* a numerical IP address in dotted decimal notation (e.g., 192.168.23.1).
* port - The port number to which to connect, in host byte order.
* file - A pointer to the name of the file to get.
* buffer - A user provided buffer to receive the file data (also
* used for the outgoing GET request
* buflen - The size of the user provided buffer
* callback - As data is obtained from the host, this function is
* to dispose of each block of file data as it is received.
*
* Returned Value:
* 0: if the GET operation completed successfully;
* -1: On a failure with errno set appropriately
*
****************************************************************************/
EXTERN int wget(FAR const char *host, uint16 port, FAR const char *file,
FAR char *buffer, int buflen, wget_callback_t callback);
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* __NET_UIP_WEBCLIENT_H */
File diff suppressed because it is too large Load Diff
-205
View File
@@ -1,205 +0,0 @@
/**
* webclient.h
* Header file for the HTTP client.
* Author: Adam Dunkels <adam@dunkels.com>
*
* Copyright (c) 2002, Adam Dunkels.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __WEBCLIENT_H__
#define __WEBCLIENT_H__
#include <sys/types.h>
#include <net/uip/uipopt.h>
#define WEBCLIENT_CONF_MAX_URLLEN 100
struct webclient_state
{
uint8 timer;
uint8 state;
uint8 httpflag;
uint16 port;
char host[40];
char file[WEBCLIENT_CONF_MAX_URLLEN];
uint16 getrequestptr;
uint16 getrequestleft;
char httpheaderline[200];
uint16 httpheaderlineptr;
char mimetype[32];
};
/* Callback function that is called from the webclient code when HTTP
* data has been received.
*
* This function must be implemented by the module that uses the
* webclient code. The function is called from the webclient module
* when HTTP data has been received. The function is not called when
* HTTP headers are received, only for the actual data.
*
* Note: This function is called many times, repetedly, when data is
* being received, and not once when all data has been received.
*
* data A pointer to the data that has been received.
* len The length of the data that has been received.
*/
void webclient_datahandler(char *data, uint16 len);
/* Callback function that is called from the webclient code when the
* HTTP connection has been connected to the web server.
*
* This function must be implemented by the module that uses the
* webclient code.
*/
void webclient_connected(void);
/* Callback function that is called from the webclient code if the
* HTTP connection to the web server has timed out.
*
* This function must be implemented by the module that uses the
* webclient code.
*/
void webclient_timedout(void);
/* Callback function that is called from the webclient code if the
* HTTP connection to the web server has been aborted by the web
* server.
*
* This function must be implemented by the module that uses the
* webclient code.
*/
void webclient_aborted(void);
/* Callback function that is called from the webclient code when the
* HTTP connection to the web server has been closed.
*
* This function must be implemented by the module that uses the
* webclient code.
*/
void webclient_closed(void);
/* Initialize the webclient module. */
void webclient_init(void);
/* Open an HTTP connection to a web server and ask for a file using
* the GET method.
*
* This function opens an HTTP connection to the specified web server
* and requests the specified file using the GET method. When the HTTP
* connection has been connected, the webclient_connected() callback
* function is called and when the HTTP data arrives the
* webclient_datahandler() callback function is called.
*
* The callback function webclient_timedout() is called if the web
* server could not be contacted, and the webclient_aborted() callback
* function is called if the HTTP connection is aborted by the web
* server.
*
* When the HTTP request has been completed and the HTTP connection is
* closed, the webclient_closed() callback function will be called.
*
* Note: If the function is passed a host name, it must already be in
* the resolver cache in order for the function to connect to the web
* server. It is therefore up to the calling module to implement the
* resolver calls and the signal handler used for reporting a resolv
* query answer.
*
* host A pointer to a string containing either a host name or
* a numerical IP address in dotted decimal notation (e.g., 192.168.23.1).
*
* port The port number to which to connect, in host byte order.
*
* file A pointer to the name of the file to get.
*
* Return: 0 if the host name could not be found in the cache, or
* if a TCP connection could not be created.
*
* Return: 1 if the connection was initiated.
*/
unsigned char webclient_get(const char *host, uint16 port, char *file);
/* Close the currently open HTTP connection. */
void webclient_close(void);
/* Obtain the MIME type of the current HTTP data stream.
*
* Return: A pointer to a string contaning the MIME type. The string
* may be empty if no MIME type was reported by the web server.
*/
char *webclient_mimetype(void);
/* Obtain the filename of the current HTTP data stream.
*
* The filename of an HTTP request may be changed by the web server,
* and may therefore not be the same as when the original GET request
* was made with webclient_get(). This function is used for obtaining
* the current filename.
*
* Return: A pointer to the current filename.
*/
char *webclient_filename(void);
/* Obtain the hostname of the current HTTP data stream.
*
* The hostname of the web server of an HTTP request may be changed
* by the web server, and may therefore not be the same as when the
* original GET request was made with webclient_get(). This function
* is used for obtaining the current hostname.
*
* Return: A pointer to the current hostname.
*/
char *webclient_hostname(void);
/* Obtain the port number of the current HTTP data stream.
*
* The port number of an HTTP request may be changed by the web
* server, and may therefore not be the same as when the original GET
* request was made with webclient_get(). This function is used for
* obtaining the current port number.
*
* Return: The port number of the current HTTP data stream, in host byte order.
*/
unsigned short webclient_port(void);
#endif /* __WEBCLIENT_H__ */