gonzui


Format: Advanced Search

t2ex/t2ex_source/kernel/sysmain/src/network_sample/httpclient.cbare sourcepermlink (0.01 seconds)

Search this content:

    1: /*
    2:  *----------------------------------------------------------------------
    3:  *    T2EX Software Package
    4:  *
    5:  *    Copyright 2012 by Ken Sakamura.
    6:  *    This software is distributed under the latest version of T-License 2.x.
    7:  *----------------------------------------------------------------------
    8:  *
    9:  *    Released by T-Engine Forum(http://www.t-engine.org/) at 2012/12/12.
   10:  *    Modified by TRON Forum(http://www.tron.org/) at 2015/06/04.
   11:  *
   12:  *----------------------------------------------------------------------
   13:  */
   14: /*
   15:  * This software package is available for use, modification, 
   16:  * and redistribution in accordance with the terms of the attached 
   17:  * T-License 2.x.
   18:  * If you want to redistribute the source code, you need to attach 
   19:  * the T-License 2.x document.
   20:  * There's no obligation to publish the content, and no obligation 
   21:  * to disclose it to the TRON Forum if you have modified the 
   22:  * software package.
   23:  * You can also distribute the modified source code. In this case, 
   24:  * please register the modification to T-Kernel traceability service.
   25:  * People can know the history of modifications by the service, 
   26:  * and can be sure that the version you have inherited some 
   27:  * modification of a particular version or not.
   28:  *
   29:  *    http://trace.tron.org/tk/?lang=en
   30:  *    http://trace.tron.org/tk/?lang=ja
   31:  *
   32:  * As per the provisions of the T-License 2.x, TRON Forum ensures that 
   33:  * the portion of the software that is copyrighted by Ken Sakamura or 
   34:  * the TRON Forum does not infringe the copyrights of a third party.
   35:  * However, it does not make any warranty other than this.
   36:  * DISCLAIMER: TRON Forum and Ken Sakamura shall not be held
   37:  * responsible for any consequences or damages caused directly or
   38:  * indirectly by the use of this software package.
   39:  *
   40:  * The source codes in bsd_source.tar.gz in this software package are 
   41:  * derived from NetBSD or OpenBSD and not covered under T-License 2.x.
   42:  * They need to be changed or redistributed according to the 
   43:  * representation of each source header.
   44:  */
   45: 
   46: /*
   47:  *      @(#)httpclient.c
   48:  *
   49:  */
   50: 
   51: #include <t2ex/socket.h>
   52: #include <tk/tkernel.h>
   53: 
   54: #include <stdio.h>
   55: #include <strings.h>
   56: 
   57: #include "util.h"
   58: #include "httpclient.h"
   59: 
   60: int http_get(const char* host, const char* path, char* buf, size_t buflen)
   61: {
   62:         int re;
   63:         int sd;
   64:         int error = 0;
   65:         in_addr_t hostaddr;
   66:         struct sockaddr_in sa;
   67:         char req[1024];
   68:         size_t offset;
   69: 
   70:         hostaddr = resolv_host(host);
   71:         if ( hostaddr == INADDR_NONE ) {
   72:                 return -1;
   73:         }
   74: 
   75:         sd = so_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
   76:         DEBUG_PRINT(("http_get: so_socket = %d(%d, %d)\n", sd, MERCD(sd), SERCD(sd)));
   77:         if ( sd < 0 ) {
   78:                 return sd;
   79:         }
   80: 
   81:         bzero(&sa, sizeof sa);
   82:         sa.sin_len = sizeof sa;
   83:         sa.sin_family = AF_INET;
   84:         sa.sin_addr.s_addr = hostaddr;
   85:         sa.sin_port = htons(80);
   86:         re = so_connect(sd, (struct sockaddr*)&sa, sizeof sa);
   87:         DEBUG_PRINT(("http_get: so_connect = %d(%d, %d)\n", re, MERCD(re), SERCD(re)));
   88:         if ( re < 0 ) {
   89:                 return re;
   90:         }
   91: 
   92:         sprintf(req, "GET %s HTTP/1.1\r\nhost: %s\r\nConnection: close\r\n\r\n", path, host);
   93:         re = so_write(sd, req, strlen(req));
   94:         DEBUG_PRINT(("http_get: so_write = %d(%d, %d)\n", re, MERCD(re), SERCD(re)));
   95: 
   96:         for(offset = 0; offset < buflen;) {
   97:                 re = so_read(sd, &buf[offset], buflen - offset);
   98:                 DEBUG_PRINT(("http_get: so_read = %d(%d, %d)\n", re, MERCD(re), SERCD(re)));
   99:                 if ( re == 0 ) {
  100:                         break;
  101:                 } else if ( re < 0 ) {
  102:                         error = re;
  103:                         break;
  104:                 }
  105:                 offset += re;
  106:         }
  107: 
  108:         so_close(sd);
  109: 
  110:         return error;
  111: }