纯C++的Socket访问Http封装类代码介绍(二)

2014-11-24 13:05:56 · 作者: · 浏览: 3
->position = b->buffer;
  43 }
  44
  45 //*
  46 // MemBufferGrow:
  47 // Double the size of the buffer that was passed to this function.
  48 //*
  49 void Request::MemBufferGrow(MemBuffer *b)
  50 {
  51 size_t sz;
  52 sz = b->position - b->buffer;
  53 b->size = b->size *2;
  54 b->buffer =(unsigned char *) realloc(b->buffer,b->size);
  55 b->position = b->buffer + sz; // readjust current position
  56 }
  57
  58 //*
  59 // MemBufferAddByte:
  60 // Add a single byte to the memory buffer, grow if needed.
  61 //*
  62 void Request::MemBufferAddByte(MemBuffer *b,unsigned char byt)
  63 {
  64 if( (size_t)(b->position-b->buffer) >= b->size )
  65 MemBufferGrow(b);
  66
  67 *(b->position++) = byt;
  68 }
  69
  70 //*
  71 // MemBufferAddBuffer:
  72 // Add a range of bytes to the memory buffer, grow if needed.
  73 //*
  74 void Request::MemBufferAddBuffer(MemBuffer *b,
  75 unsigned char *buffer, size_t size)
  76 {
  77 while( ((size_t)(b->position-b->buffer)+size) >= b->size )
  78 MemBufferGrow(b);
  79
  80 memcpy(b->position,buffer,size);
  81 b->position+=size;
  82 }
  83
  84 //*
  85 // GetHostAddress:
  86 // Resolve using DNS or similar(WINS,etc) the IP
  87 // address for a domain name such as www.wdj.com.
  88 //*
  89 DWORD Request::GetHostAddress(LPCSTR host)
  90 {
  91 struct hostent *phe;
  92 char *p;
  93
  94 phe = gethostbyname( host );
  95
  96 if(phe==NULL)
  97 return 0;
  98
  99 p = *phe->h_addr_list;
  100 return *((DWORD*)p);
  101 }
  102


103 //*
  104 // SendString:
  105 // Send a string(null terminated) over the specified socket.
  106 //*
  107 void Request::SendString(SOCKET sock,LPCSTR str)
  108 {
  109 send(sock,str,strlen(str),0);
  110 }
  111
  112 //*
  113 // ValidHostChar:
  114 // Return TRUE if the specified character is valid
  115 // for a host name, i.e. A-Z or 0-9 or -.:
  116 //*
  117 BOOL Request::ValidHostChar(char ch)
  118 {
  119 return( isalpha(ch) || isdigit(ch)
  120 || ch==- || ch==. || ch==: );
  121 }
  122
  123
  124 //*
  125 // ParseURL:
  126 // Used to break apart a URL such as
  127 // http://www.localhost.com:80/TestPost.htm into protocol, port, host and request.
  128 //*
  129 void Request::ParseURL(string url,LPSTR protocol,int lpr