Copy of NetServicesMin with the HTTP Client library. Includes modification for HTTP servers which send the HTTP status line in its own packet.

Committer:
andrewbonney
Date:
Thu May 26 10:02:40 2011 +0000
Revision:
0:18dd877d2c77

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewbonney 0:18dd877d2c77 1
andrewbonney 0:18dd877d2c77 2 /*
andrewbonney 0:18dd877d2c77 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
andrewbonney 0:18dd877d2c77 4
andrewbonney 0:18dd877d2c77 5 Permission is hereby granted, free of charge, to any person obtaining a copy
andrewbonney 0:18dd877d2c77 6 of this software and associated documentation files (the "Software"), to deal
andrewbonney 0:18dd877d2c77 7 in the Software without restriction, including without limitation the rights
andrewbonney 0:18dd877d2c77 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
andrewbonney 0:18dd877d2c77 9 copies of the Software, and to permit persons to whom the Software is
andrewbonney 0:18dd877d2c77 10 furnished to do so, subject to the following conditions:
andrewbonney 0:18dd877d2c77 11
andrewbonney 0:18dd877d2c77 12 The above copyright notice and this permission notice shall be included in
andrewbonney 0:18dd877d2c77 13 all copies or substantial portions of the Software.
andrewbonney 0:18dd877d2c77 14
andrewbonney 0:18dd877d2c77 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
andrewbonney 0:18dd877d2c77 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
andrewbonney 0:18dd877d2c77 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
andrewbonney 0:18dd877d2c77 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
andrewbonney 0:18dd877d2c77 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
andrewbonney 0:18dd877d2c77 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
andrewbonney 0:18dd877d2c77 21 THE SOFTWARE.
andrewbonney 0:18dd877d2c77 22 */
andrewbonney 0:18dd877d2c77 23
andrewbonney 0:18dd877d2c77 24 #include "lwipNetDnsRequest.h"
andrewbonney 0:18dd877d2c77 25 #include "lwip/err.h" //err_t, ERR_xxx
andrewbonney 0:18dd877d2c77 26 #include "lwip/dns.h"
andrewbonney 0:18dd877d2c77 27
andrewbonney 0:18dd877d2c77 28 #include "netCfg.h"
andrewbonney 0:18dd877d2c77 29 #if NET_LWIP_STACK
andrewbonney 0:18dd877d2c77 30
andrewbonney 0:18dd877d2c77 31 //#define __DEBUG
andrewbonney 0:18dd877d2c77 32 #include "dbg/dbg.h"
andrewbonney 0:18dd877d2c77 33
andrewbonney 0:18dd877d2c77 34 LwipNetDnsRequest::LwipNetDnsRequest(const char* hostname) : NetDnsRequest(hostname), m_state(LWIPNETDNS_START), m_cbFired(false), m_closing(false)
andrewbonney 0:18dd877d2c77 35 {
andrewbonney 0:18dd877d2c77 36 DBG("New LwipNetDnsRequest %p\n", this);
andrewbonney 0:18dd877d2c77 37 }
andrewbonney 0:18dd877d2c77 38
andrewbonney 0:18dd877d2c77 39 LwipNetDnsRequest::LwipNetDnsRequest(Host* pHost) : NetDnsRequest(pHost), m_state(LWIPNETDNS_START), m_cbFired(false), m_closing(false)
andrewbonney 0:18dd877d2c77 40 {
andrewbonney 0:18dd877d2c77 41 DBG("New LwipNetDnsRequest %p\n", this);
andrewbonney 0:18dd877d2c77 42 }
andrewbonney 0:18dd877d2c77 43
andrewbonney 0:18dd877d2c77 44 LwipNetDnsRequest::~LwipNetDnsRequest()
andrewbonney 0:18dd877d2c77 45 {
andrewbonney 0:18dd877d2c77 46 DBG("LwipNetDnsRequest %p destroyed\n", this);
andrewbonney 0:18dd877d2c77 47 }
andrewbonney 0:18dd877d2c77 48
andrewbonney 0:18dd877d2c77 49 /*
andrewbonney 0:18dd877d2c77 50 Main useful function here (see lwip/dns.h):
andrewbonney 0:18dd877d2c77 51 err_t dns_gethostbyname(const char *hostname, ip_addr_t *addr,
andrewbonney 0:18dd877d2c77 52 dns_found_callback found, void *callback_arg);
andrewbonney 0:18dd877d2c77 53 */
andrewbonney 0:18dd877d2c77 54
andrewbonney 0:18dd877d2c77 55 //Execute request & return OK if found, NOTFOUND or ERROR on error, or PROCESSING if the request has not completed yet
andrewbonney 0:18dd877d2c77 56 void LwipNetDnsRequest::poll()
andrewbonney 0:18dd877d2c77 57 {
andrewbonney 0:18dd877d2c77 58 err_t err;
andrewbonney 0:18dd877d2c77 59 switch(m_state)
andrewbonney 0:18dd877d2c77 60 {
andrewbonney 0:18dd877d2c77 61 case LWIPNETDNS_START: //First req, let's call dns_gethostbyname
andrewbonney 0:18dd877d2c77 62 ip_addr_t ipStruct;
andrewbonney 0:18dd877d2c77 63 err = dns_gethostbyname(m_hostname, &ipStruct, LwipNetDnsRequest::sFoundCb, (void*) this );
andrewbonney 0:18dd877d2c77 64 if( err == ERR_OK )
andrewbonney 0:18dd877d2c77 65 {
andrewbonney 0:18dd877d2c77 66 m_ip = IpAddr(&ipStruct);
andrewbonney 0:18dd877d2c77 67 m_state = LWIPNETDNS_OK;
andrewbonney 0:18dd877d2c77 68 DBG("DNS: Ip found in cache.\n");
andrewbonney 0:18dd877d2c77 69 }
andrewbonney 0:18dd877d2c77 70 else if( err == ERR_INPROGRESS)
andrewbonney 0:18dd877d2c77 71 {
andrewbonney 0:18dd877d2c77 72 DBG("DNS: Processing.\n");
andrewbonney 0:18dd877d2c77 73 m_state = LWIPNETDNS_PROCESSING;
andrewbonney 0:18dd877d2c77 74 }
andrewbonney 0:18dd877d2c77 75 else //Likely ERR_VAL
andrewbonney 0:18dd877d2c77 76 {
andrewbonney 0:18dd877d2c77 77 DBG("DNS: Error on init.\n");
andrewbonney 0:18dd877d2c77 78 m_state = LWIPNETDNS_ERROR;
andrewbonney 0:18dd877d2c77 79 }
andrewbonney 0:18dd877d2c77 80 break;
andrewbonney 0:18dd877d2c77 81 case LWIPNETDNS_PROCESSING:
andrewbonney 0:18dd877d2c77 82 break; //Nothing to do, DNS is polled on interrupt
andrewbonney 0:18dd877d2c77 83 case LWIPNETDNS_OK:
andrewbonney 0:18dd877d2c77 84 if(!m_cbFired)
andrewbonney 0:18dd877d2c77 85 {
andrewbonney 0:18dd877d2c77 86 DBG("DNS: Ip found.\n");
andrewbonney 0:18dd877d2c77 87 m_cbFired = true;
andrewbonney 0:18dd877d2c77 88 onReply(NETDNS_FOUND); //Raise callback
andrewbonney 0:18dd877d2c77 89 }
andrewbonney 0:18dd877d2c77 90 break;
andrewbonney 0:18dd877d2c77 91 case LWIPNETDNS_NOTFOUND:
andrewbonney 0:18dd877d2c77 92 if(!m_cbFired)
andrewbonney 0:18dd877d2c77 93 {
andrewbonney 0:18dd877d2c77 94 DBG("DNS: could not be resolved.\n");
andrewbonney 0:18dd877d2c77 95 m_cbFired = true;
andrewbonney 0:18dd877d2c77 96 onReply(NETDNS_NOTFOUND); //Raise callback
andrewbonney 0:18dd877d2c77 97 }
andrewbonney 0:18dd877d2c77 98 break;
andrewbonney 0:18dd877d2c77 99 case LWIPNETDNS_ERROR:
andrewbonney 0:18dd877d2c77 100 default:
andrewbonney 0:18dd877d2c77 101 if(!m_cbFired)
andrewbonney 0:18dd877d2c77 102 {
andrewbonney 0:18dd877d2c77 103 DBG("DNS: Error.\n");
andrewbonney 0:18dd877d2c77 104 m_cbFired = true;
andrewbonney 0:18dd877d2c77 105 onReply(NETDNS_ERROR); //Raise callback
andrewbonney 0:18dd877d2c77 106 }
andrewbonney 0:18dd877d2c77 107 break;
andrewbonney 0:18dd877d2c77 108 }
andrewbonney 0:18dd877d2c77 109 if(m_closing && (m_state!=LWIPNETDNS_PROCESSING)) //Check wether the closure has been reqd
andrewbonney 0:18dd877d2c77 110 {
andrewbonney 0:18dd877d2c77 111 DBG("LwipNetDnsRequest: Closing in poll()\n");
andrewbonney 0:18dd877d2c77 112 NetDnsRequest::close();
andrewbonney 0:18dd877d2c77 113 }
andrewbonney 0:18dd877d2c77 114 }
andrewbonney 0:18dd877d2c77 115
andrewbonney 0:18dd877d2c77 116 void LwipNetDnsRequest::close()
andrewbonney 0:18dd877d2c77 117 {
andrewbonney 0:18dd877d2c77 118 DBG("LwipNetDnsRequest: Close req\n");
andrewbonney 0:18dd877d2c77 119 if(m_state!=LWIPNETDNS_PROCESSING)
andrewbonney 0:18dd877d2c77 120 {
andrewbonney 0:18dd877d2c77 121 DBG("LwipNetDnsRequest: Closing in close()\n");
andrewbonney 0:18dd877d2c77 122 NetDnsRequest::close();
andrewbonney 0:18dd877d2c77 123 }
andrewbonney 0:18dd877d2c77 124 else //Cannot close rightaway, waiting for callback from underlying layer
andrewbonney 0:18dd877d2c77 125 {
andrewbonney 0:18dd877d2c77 126 m_closing = true;
andrewbonney 0:18dd877d2c77 127 }
andrewbonney 0:18dd877d2c77 128 }
andrewbonney 0:18dd877d2c77 129
andrewbonney 0:18dd877d2c77 130 void LwipNetDnsRequest::foundCb(const char *name, ip_addr_t *ipaddr)
andrewbonney 0:18dd877d2c77 131 {
andrewbonney 0:18dd877d2c77 132 if( ipaddr == NULL )
andrewbonney 0:18dd877d2c77 133 {
andrewbonney 0:18dd877d2c77 134 DBG("LwipNetDnsRequest: Callback: Name not found\n");
andrewbonney 0:18dd877d2c77 135 m_state = LWIPNETDNS_NOTFOUND;
andrewbonney 0:18dd877d2c77 136 return;
andrewbonney 0:18dd877d2c77 137 }
andrewbonney 0:18dd877d2c77 138 DBG("LwipNetDnsRequest: Callback: Resolved\n");
andrewbonney 0:18dd877d2c77 139 m_ip = IpAddr(ipaddr);
andrewbonney 0:18dd877d2c77 140 m_state = LWIPNETDNS_OK;
andrewbonney 0:18dd877d2c77 141 }
andrewbonney 0:18dd877d2c77 142
andrewbonney 0:18dd877d2c77 143
andrewbonney 0:18dd877d2c77 144 void LwipNetDnsRequest::sFoundCb(const char *name, ip_addr_t *ipaddr, void *arg)
andrewbonney 0:18dd877d2c77 145 {
andrewbonney 0:18dd877d2c77 146 DBG("LwipNetDnsRequest: Static callback\n");
andrewbonney 0:18dd877d2c77 147 LwipNetDnsRequest* pMe = (LwipNetDnsRequest*) arg;
andrewbonney 0:18dd877d2c77 148 return pMe->foundCb( name, ipaddr );
andrewbonney 0:18dd877d2c77 149 }
andrewbonney 0:18dd877d2c77 150
andrewbonney 0:18dd877d2c77 151 #endif
andrewbonney 0:18dd877d2c77 152