File: simple_request.htb

package info (click to toggle)
httest 2.4.23-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,876 kB
  • sloc: ansic: 19,553; sh: 10,550; xml: 1,047; makefile: 510; lisp: 145; perl: 31
file content (111 lines) | stat: -rw-r--r-- 2,596 bytes parent folder | download | duplicates (6)
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
# This DSL is for testing Applications.
# Implements a request method with automatic cookie handling and follow 
# redirect. It is much like get a page with your favorit browser.
#

MODULE SIMPLE

##
# Internal command do not use directly
BLOCK _FOLLOW RECURSION
  _IF "$RECURSION" GT 20 
    _DEBUG Give up more than 20 redirects!
    _EXPECT headers "!HTTP/.\.. 302"
  _END IF
  _GREP headers "(HTTP/.\.. .*)" STATUS
  _GREP headers "Location: (.*)" LOCATION
  _RECORD RES ALL
  _WAIT
  _IF "${STATUS}" MATCH "302"
    _IF "YES${LOCATION}" NOT MATCH "^YES$"
      _SIMPLE:REQUEST GET $LOCATION
      __
      _OP $RECURSION ADD 1 RECURSION
      _SIMPLE:FOLLOW $RECURSION
    _END IF
  _END IF
END

##
# Send a request
# @param METHOD IN defines the method GET, POST, HEAD, ...
# @param URL IN defines the url to send to
BLOCK _REQUEST METHOD URL
  _GREP VAR(URL) "(https?:)" SCHEMA
  _IF "$SCHEMA" NOT MATCH "^https?:" 
    _SET SCHEMA=relative
  _END IF
  _IF "$SCHEMA" NOT MATCH "^relative$"
    _MATCH VAR(URL) "(https?)://([^/]+)(/.*)" SCHEMA HOST URI
    _GREP VAR(HOST) "([^:]+):?(.*)?" NAME PORT
    _SET _HOST=$NAME:$PORT
    _IF "$SCHEMA" MATCH "^https$"
      _SET _PORT=SSL:$PORT
      _IF "YES${PORT}" MATCH "^YES$"
        _SET PORT=443
        _SET _PORT=SSL:443
        _SET _HOST=$NAME
      _END IF
    _ELSE
      _IF "YES$PORT" NOT MATCH "^YES$"
        _SET _PORT=$PORT
      _ELSE
        _SET PORT=80
        _SET _PORT=80
        _SET _HOST=$NAME
      _END IF
    _END IF
    _REQ $NAME $_PORT
  _ELSE
    _MATCH VAR(URL) "(/.*)" URI
  _END IF
  __$METHOD $URI HTTP/1.1
  __Host: $_HOST
  __User-Agent: Mozilla
  __Cookie: AUTO
END

##
# Send a g GET request
# @param URL IN url to send the GET request to
# @note: automaticaly follow redirects
# @example: _SIMPLE:GET https://foo.bar.ch/bla bla HTTP/1.1
BLOCK _GET URL
  _AUTO_CLOSE on
  _SIMPLE:REQUEST GET $URL
  __
  _SIMPLE:FOLLOW 0
  _PLAY SOCKET
END

##
# Send a g GET request
# @param URL IN url to send the GET request to
# @example: _SIMPLE:POST https://foo.bar.ch/bla bla HTTP/1.1
#           __
#           _-foo=bar&bla=blabla
BLOCK _POST URL
  _AUTO_CLOSE on
  _SIMPLE:REQUEST POST $URL
  __Content-Length: AUTO
END

##
# crawl all <a href=...> 
# @param URL IN url to crawl
# @param BLOCK IN block name to execute on every found href
# @note: BLOCK signature must contain one parameter
BLOCK _CRAWL URL BLOCK
  _AUTO_CLOSE on
  _SIMPLE:GET $URL
  _WAIT BUF

  _HTML:PARSE VAR(BUF)
  _HTML:XPATH count(//a) COUNT

  _LOOP $COUNT I=1
    _HTML:XPATH //a[$I]/@href RESULT
    $BLOCK VAR(RESULT)
  _END
END