File: httpencode.cpp

package info (click to toggle)
ctorrent 1.3.4.dnh3.3.2-5
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,240 kB
  • sloc: cpp: 13,161; sh: 675; ansic: 383; makefile: 6
file content (156 lines) | stat: -rw-r--r-- 3,082 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "./def.h"
#include <sys/types.h>

#include "./httpencode.h"

#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#include "./config.h"

#if !defined(HAVE_STRNSTR) || !defined(HAVE_STRNCASECMP)
#include "compat.h"
#endif

static void url_encode_char(char *b,char c)
{
  char HEX_TABLE[] = "0123456789ABCDEF";
  b[0] = '%';
  b[1] = HEX_TABLE[(c >> 4) & 0x0F];
  b[2] = HEX_TABLE[c & 0x0F];
}

char* Http_url_encode(char *s,const char *b,size_t n)
{
  size_t r,i;
  for(r = 0,i = 0 ; i < n; i++){
    if( !(b[i] & ~0x7f) &&                   // quick ASCII test
        ((b[i] >= 0x41 && b[i] <= 0x5a) ||   // A-Z [ASCII]
         (b[i] >= 0x61 && b[i] <= 0x7a) ||   // a-z
         (b[i] >= 0x30 && b[i] <= 0x39)) ){  // 0-9
      s[r] = b[i];
      r++;
    }else{
      url_encode_char(s + r, b[i]);
      r += 3;
    }
  }
  s[r] = '\0';
  return s;
}

int Http_url_analyse(const char *url,char *host,int *port,char *path)
{
  const char *p;
  int r;
  *port = 80;	/* default port 80 */
  p = strstr(url,"://");
  if( !p ) 
    p = url;
  else
    p += 3;

  /* host */
  for(; *p && (isalnum(*p) || *p == '.' || *p == '-'); p++, host++) 
    *host = *p;
  *host = '\0';

  if( *p == ':' ){
    /* port */
    p++;
    for( r = 0; p[r] >= '0' && p[r] <= '9' && r < 6; r++) ;

    if( !r ) return -1;
    *port = atoi(p);
    if(*port > 65536) return -1;
    p += r;
  }

  /* path */
  if( *p != '/' ) return -1;
  for( ; *p; p++,path++) *path = *p;
  *path = '\0';
  return 0;
}

size_t Http_split(char *b,size_t n,char **pd,size_t *dlen)
{
  char *p;
  size_t addtion, hlen;

  hlen = 0;

  if( n < 16 ) return 0;	// ����̫С����������һ��HTML����

  if(strncasecmp(b,"HTTP/",5) != 0){
    return 0;			// û��HTML�ײ�????
    /* message without http header */
    //*pd = b;
    //*dlen = n;
  }else{
    if( p = strnstr(b,"\r\n\r\n",n) ) addtion = 4;
    else if( p = strnstr(b,"\n\n",n) ) addtion = 2;

    if( p ){
      hlen = p - b;
      *pd = ( p + addtion );
      *dlen = n - hlen - addtion;
    }else{		// ֻ���ײ���Ϣ????
      hlen = n;
      *pd = (char*) 0;
      *dlen = 0;
    }
  }
  return hlen;
}

int Http_reponse_code(const char *b,size_t n)
{
  int r = -1;

  for(; n && *b != ' ' && *b !='\r' && *b != '\n'; b++,n--) ;
  if( !n || *b != ' ') r = -1;
  else{
          r = atoi(b);
          if( r < 100 || r > 600 ) r = -1;
  }
  return r;
}

int Http_get_header(const char *b,int n,const char *header,char *v)
{
  const char *e;
  char h[64];
  int r,header_len;

  strcpy(h,header);
  strcat(h,": ");
  header_len = strlen(h);

  /* remove status line. */
  e = strchr(b,'\n');
  if( !e ) return -1;
  e++;
  n -= (e - b);
  b = e;

  for(; n >= 0; ){
    e = strchr(b,'\n');  
    if( !e ) r = n;		/* last line */
    else{r = e - b ; r++;}
  
    if( r > header_len ){
      if( strncasecmp(b, h, header_len) == 0){
        /* header found */
        b += header_len;
        for(; *b != '\r' && *b != '\n'; v++,b++) *v = *b;
        *v = '\0';
        return 0;
      }
    }
    b += r;
    n -= r;
  } /* end for */
  return -1;
}