1 /******************************************************************************
2 *
3 * Copyright (C) 2004-2008, The Gentee Group. All rights reserved.
4 * This file is part of the Gentee open source project - http://www.gentee.com.
5 *
6 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE GENTEE LICENSE ("AGREEMENT").
7 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE CONSTITUTES RECIPIENTS
8 * ACCEPTANCE OF THE AGREEMENT.
9 *
10 * Author: Alexey Krivonogov ( gentee )
11 *
12 ******************************************************************************/
13
14 /*-----------------------------------------------------------------------------
15 * Id: str_ihead F2
16 *
17 * Summary: Getting a header. The method is used to get the message header.
18 It will be written to the string for which the method was called.
19 Besides, the header will be deleted from the data object.
20 *
21 * Params: data - The buffer of the string containing the data being processed.
22 *
23 * Return: #lng/retobj#
24 *
25 -----------------------------------------------------------------------------*/
26
27 method str str.ihead( buf data )
28 {
29 uint i
30
31 this.clear()
32
33 while !"\00D\00A".eqlen( data.ptr() + i )
34 {
35 i = data->str.findchfrom( 0x0A, i ) + 1
36 if i >= data.use : return this
37 }
38 this.copy( data.ptr(), i + 2 )
39 data.del( 0, i + 2 )
40
41 return this
42 }
43
44 /*-----------------------------------------------------------------------------
45 * Id: str_iurl F2
46 *
47 * Summary: The method is used to parse a URL address.
48 *
49 * Params: host - The string for getting the host name.
50 port - The string for getting the port.
51 path - The string for getting the relative path.
52 *
53 * Return: 1 is returned if the FTP protocol was specified. Otherwise, 0 is
54 returned.
55 *
56 -----------------------------------------------------------------------------*/
57
58 method uint str.iurl( str host, str port, str path )
59 {
60 arrstr names
61 arrstr hosts
62 str stemp = this
63 uint ret = $INET_HTTP
64
65 if !*this : return ret
66 if "http://".eqlenign( stemp.ptr()) : stemp.del( 0, 7 )
67 elif "ftp://".eqlenign( stemp.ptr())
68 {
69 stemp.del( 0, 6 )
70 ret = $INET_FTP
71 }
72 stemp.split( names, '/', $SPLIT_FIRST )
73 names[0].split( hosts, ':', $SPLIT_FIRST )
74 host = hosts[0]
75 if *hosts > 1 : port = hosts[1]
76 if *names > 1 : path = names[1]
77
78 return ret
79 }
80
81 /*-----------------------------------------------------------------------------
82 * Id: str_ihttpinfo F2
83 *
84 * Summary: Processing a header. The method processes a string as an HTTP
85 header and writes data it gets into the #a(thttpinfo) structure.
86 *
87 * Params: hi - The variable of the #a( thttpinfo ) type for getting the /
88 results.
89 *
90 * Return: #lng/retf#
91 *
92 -----------------------------------------------------------------------------*/
93
94 method uint str.ihttpinfo( httpinfo hi )
95 {
96 uint i
97 arrstr month
98 arrstr day
99 arrstr val
100 arrstr lines
101
102 this.split( lines, 0xA, $SPLIT_NOSYS )
103 if !*lines || !"HTTP".eqlenign( this.ptr() ) : return 0
104 lines[0].split( val, ' ', $SPLIT_NOSYS )
105 hi.code = uint( val[1] )
106
107 month += "Jan"; month += "Feb"; month += "Mar"; month += "Apr"
108 month += "May"; month += "Jun"; month += "Jul"; month += "Aug"
109 month += "Sep"; month += "Oct"; month += "Nov"; month += "Dec"
110
111 day += "Sun"; day += "Mon"; day += "Tue"; day += "Wed"
112 day += "Thu"; day += "Fri"; day += "Sat"
113
114 foreach cur, lines
115 {
116 cur.split( val, ':', $SPLIT_NOSYS | $SPLIT_FIRST )
117
118 if *val == 1 : continue
119 switch val[0]
120 {
121 case "Content-Length" : hi.size = val[1]
122 case "Location" : hi.location = val[1]
123 case "Last-Modified"
124 {
125 uint mode tmode
126 arrstr dtime
127 str stemp
128 /* 0 - RFC 1123 1 - RFC 850 2 - ASCTIME */
129 ( stemp = val[1] ).split( val, ' ', $SPLIT_NOSYS )
130 if *val < 4 : continue
131 if !val[0].islast( ',' ) : mode = 2
132 elif *val[0] > 4 : mode = 1
133
134 fornum i = 0, *day
135 {
136 if day[i].eqlenign( val[0] )
137 {
138 hi.dt.dayofweek = i
139 break
140 }
141 }
142 switch mode
143 {
144 case 0
145 {
146 hi.dt.day = uint( val[1] )
147 stemp = val[2]
148 hi.dt.year = uint( val[ 3 ] )
149 tmode = 4
150 }
151 case 1
152 {
153 val[1].split( dtime, '-', $SPLIT_NOSYS )
154 hi.dt.day = uint( dtime[ 0 ] )
155 stemp = dtime[1]
156 hi.dt.year = uint( dtime[ 2 ] ) + 1900
157 if hi.dt.year < 1970 : hi.dt.year += 100
158 tmode = 2
159 }
160 case 2
161 {
162 hi.dt.day = uint( val[2] )
163 hi.dt.year = uint( val[ 4 ] )
164 stemp = val[1]
165 tmode = 3
166 }
167 }
168 fornum i = 0, *month
169 {
170 if stemp %== month[i]
171 {
172 hi.dt.month = i + 1
173 break
174 }
175 }
176 val[ tmode ].split( dtime, ':', $SPLIT_NOSYS )
177 hi.dt.hour = uint( dtime[0] )
178 hi.dt.minute = uint( dtime[1] )
179 hi.dt.second = uint( dtime[2] )
180 }
181 }
182 }
183 return 1
184 }
185
186 /*-----------------------------------------------------------------------------
187 ** Id: str_iencoding F2
188 *
189 * Summary: Recoding a string. The method recodes the specified string in order
190 to send it using the POST method. Spaces are replaced with '+',
191 special characters are replaced with their hexadecimal
192 representations #b(%XX). The result will be written to the string
193 for which the method was called.
194 *
195 * Params: src - The string for recoding.
196 *
197 * Return: #lng/retobj#
198 *
199 -----------------------------------------------------------------------------*/
200
201 method str str.iencoding( str src )
202 {
203 uint i
204
205 fornum i, *src
206 {
207 switch src[i]
208 {
209 case '%', '&', 0x27, '+', '=', '?'
210 {
211 this.appendch('%')
212 //hex2stru( this, src[i] )
213 this.hexu( src[i] )
214 }
215 case ' ' : this.appendch('+')
216 default : this.appendch( src[i] )
217 }
218 }
219 return this
220 }
Edit