EnglishРусский  

   ..

   ping.g

   proxy.g

   socket.g

   strinet.g

The project is closed! You can look at a new scripting language. It is available on GitHub.
Also, try our open source cross-platform automation software.

Ads

Installer and installation software
Commercial and Freeware installers.

source\lib\socket\strinet.g
  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 method datetime.frominet( str value )
 82 {
 83    uint i
 84    arrstr   month 
 85    arrstr   day
 86    arrstr   arval
 87     
 88    month += "Jan"; month += "Feb"; month += "Mar"; month += "Apr"
 89    month += "May"; month += "Jun"; month += "Jul"; month += "Aug"
 90    month += "Sep"; month += "Oct"; month += "Nov"; month += "Dec"
 91 
 92    day += "Sun"; day += "Mon"; day += "Tue"; day += "Wed"
 93    day += "Thu"; day += "Fri"; day += "Sat"
 94    
 95    uint mode tmode
 96    arrstr  dtime 
 97    str  stemp
 98    /* 0 - RFC 1123 1 - RFC 850 2 - ASCTIME */
 99    
100    ( stemp = value ).split( arval, ' ', $SPLIT_NOSYS )
101    if *arval < 4 : return
102    if !arval[0].islast( ',' ) : mode = 2
103    elif *arval < 5 : mode = 1
104 
105    fornum i = 0, *day
106    {
107       if day[i].eqlenign( arval[0] )
108       {
109          this.dayofweek = i
110          break
111       }
112    }          
113    switch mode
114    {
115       case 0 
116       {
117          this.day = uint( arval[1] )
118          stemp = arval[2]
119          this.year = uint( arval[ 3 ] )
120          tmode = 4
121       }
122       case 1 
123       { 
124          arval[1].split( dtime, '-', $SPLIT_NOSYS )
125          this.day = uint( dtime[ 0 ] )
126          stemp = dtime[1]
127          if *dtime[2] == 2
128          {
129             this.year = uint( dtime[ 2 ] ) + 1900
130             if this.year < 1970 : this.year += 100            
131          }
132          else : this.year = uint( dtime[2] )
133          tmode = 2
134       }
135       case 2 
136       {
137          this.day = uint( arval[2] )
138          this.year = uint( arval[ 4 ] )
139          stemp = arval[1]
140          tmode = 3
141       }
142    }
143    fornum i = 0, *month
144    {
145       if stemp %== month[i] 
146       {
147          this.month = i + 1
148          break
149       } 
150    }
151    arval[ tmode ].split( dtime, ':', $SPLIT_NOSYS )
152    this.hour = uint( dtime[0] )
153    this.minute = uint( dtime[1] )
154    this.second = uint( dtime[2] )
155 }
156 
157 /*-----------------------------------------------------------------------------
158 * Id: str_ihttpinfo F2
159 *
160 * Summary: Processing a header. The method processes a string as an HTTP 
161            header and writes data it gets into the #a(thttpinfo) structure.  
162 *
163 * Params: hi - The variable of the #a( thttpinfo ) type for getting the /
164                results.
165 *  
166 * Return: #lng/retf# 
167 *
168 -----------------------------------------------------------------------------*/
169 method uint str.ihttpinfo( httpinfo hi )
170 {
171    uint  i   
172    arrstr   val 
173    arrstr   lines 
174    
175    this.split( lines, 0xA, $SPLIT_NOSYS )
176    if !*lines || !"HTTP".eqlenign( this.ptr() ) : return 0
177    lines[0].split( val, ' ', $SPLIT_NOSYS )
178    hi.code = uint( val[1] )
179      
180    foreach cur, lines
181    {
182       cur.split( val, ':', $SPLIT_NOSYS | $SPLIT_FIRST )
183             
184       if *val == 1 : continue
185       switch val[0]
186       {
187          case "Content-Length" : hi.size = val[1]                      
188          case "Location" : hi.location = val[1]
189          case "Last-Modified" 
190          {
191             hi.dt.frominet( val[1] ) 
192          }
193       }
194    }
195    return 1
196 }
197 
198 /*-----------------------------------------------------------------------------
199 ** Id: str_iencoding F2
200 *
201 * Summary: Recoding a string. The method recodes the specified string in order
202            to send it using the POST method. Spaces are replaced with '+',
203            special characters are replaced with their hexadecimal
204            representations #b(%XX). The result will be written to the string 
205            for which the method was called. 
206 *
207 * Params: src - The string for recoding.      
208 *  
209 * Return: #lng/retobj# 
210 *
211 -----------------------------------------------------------------------------*/
212 
213 method str str.iencoding( str src )
214 {
215    uint i 
216    
217    fornum i, *src
218    {
219       switch src[i]
220       {
221          case '%', '&', 0x27, '+', '=', '?' 
222          {
223             this.appendch('%')
224             //hex2stru( this, src[i] )
225             this.hexu( src[i] )
226          }
227          case ' ' : this.appendch('+')
228          default : this.appendch( src[i] )
229       }   
230    }
231    return this
232 }