« 前一篇:新邮箱地址
后一篇:BaiDu招股书 »

Basic WWW-Authentication @ 8/9/2005

学习类
from: http://cvs.sourceforge.net/viewcvs.py/xine/xine-lib/src/input/input_http.c?rev=1.110
static int http_basicauth (const char *user, const char *password, char* dest, int len) {
  static char *enctable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  char        *tmp;
  char        *sptr;
  char        *dptr;
  int          totlen;
  int          enclen;
  int          count;
 
  totlen = strlen (user) + 1;
  if(password != NULL)
    totlen += strlen (password);
 
  enclen = ((totlen + 2) / 3 ) * 4 + 1;
 
  if (len < enclen)
    return -1;
 
  tmp = malloc (sizeof(char) * (totlen + 1));
  strcpy (tmp, user);
  strcat (tmp, ":");
  if (password != NULL)
    strcat (tmp, password); 
 
  count = strlen(tmp);
  sptr = tmp;
  dptr = dest;
  while (count >= 3) {
    dptr[0] = enctable[(sptr[0] & 0xFC) >> 2];
    dptr[1] = enctable[((sptr[0] & 0x3) << 4) | ((sptr[1] & 0xF0) >> 4)];
    dptr[2] = enctable[((sptr[1] & 0x0F) << 2) | ((sptr[2] & 0xC0) >> 6)];
    dptr[3] = enctable[sptr[2] & 0x3F];
    count -= 3;
    sptr += 3;
    dptr += 4;
  }
 
  if (count > 0) {
    dptr[0] = enctable[(sptr[0] & 0xFC) >> 2];
    dptr[1] = enctable[(sptr[0] & 0x3) << 4];
    dptr[2] = '=';
   
    if (count > 1) {
      dptr[1] = enctable[((sptr[0] & 0x3) << 4) | ((sptr[1] & 0xF0) >> 4)];
      dptr[2] = enctable[(sptr[1] & 0x0F) << 2];
    }
   
    dptr[3] = '=';
    dptr += 4;
  }
 
  dptr[0] = '\0';
 
  free(tmp);
  return 0;
}


其实,说白了,就是在Header中加入:
Authorization: Basic (<username:password>的base64编码)

建议参考http://dev.w3.org/cvsweb/2001/head/来看。

关于http协议,请参看rfc2616
发布于 8/9/2005 0:54:14 | 评论:0

看帖要回帖...

categories
archives
links
statistics
  • 网志数:1167
  • 评论数:2011