LibItsHttp_Functions.ttcn 6.74 KB
Newer Older
garciay's avatar
garciay committed
1
2
module LibItsHttp_Functions {
    
Yann Garcia's avatar
Yann Garcia committed
3
4
5
6
7
    // LibCommon
    import from LibCommon_Time all;
    import from LibCommon_VerdictControl all;
    import from LibCommon_Sync all;
    
garciay's avatar
garciay committed
8
9
10
    // LibHttp
    import from LibItsHttp_TypesAndValues all;
    import from LibItsHttp_Pics all;
11
    import from LibItsHttp_Pixits all;
Yann Garcia's avatar
Yann Garcia committed
12
    import from LibItsHttp_TestSystem all;
garciay's avatar
garciay committed
13
    
Yann Garcia's avatar
Yann Garcia committed
14
15
16
    group http_preambles {
      
      /**
Yann Garcia's avatar
Yann Garcia committed
17
       * @desc    Setups default configuration
Yann Garcia's avatar
Yann Garcia committed
18
       * @param   p_certificate_id The certificate identifier the TA shall use in case of secured IUT
Yann Garcia's avatar
Yann Garcia committed
19
20
       */
      function f_cfUp(
Yann Garcia's avatar
Yann Garcia committed
21
                      in charstring p_certificate_id
Yann Garcia's avatar
Yann Garcia committed
22
23
24
25
26
27
28
29
      ) runs on HttpComponent /* TITAN TODO: system HttpTestAdapter */ {
          
          map(self:httpPort, system:httpPort);
          f_connect4SelfOrClientSync();
          
      } // End of function f_cfUp
      
    } // End of group http_preambles 
garciay's avatar
garciay committed
30
    
Yann Garcia's avatar
Yann Garcia committed
31
32
33
34
35
36
    group http_postambles {
      
      /**
       * @desc    Deletes default configuration 
       */
      function f_cfDown() runs on HttpComponent /* TITAN TODO: system HttpTestAdapter */ {
garciay's avatar
garciay committed
37
          
Yann Garcia's avatar
Yann Garcia committed
38
39
40
41
42
43
44
45
46
47
          unmap(self:httpPort, system:httpPort);
          f_disconnect4SelfOrClientSync();
          
      } // End of function f_cfDown
      
    } // End of group http_postambles 
    
    group http_headers {
      
      function f_init_default_headers_list(
48
49
                                           in charstring p_header_content_type := PICS_HEADER_CONTENT_TYPE,
                                           in charstring p_header_content_text := "",
Yann Garcia's avatar
Yann Garcia committed
50
                                           out Headers p_headers
Yann Garcia's avatar
Yann Garcia committed
51
      ) {
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
        var integer v_i := 0;
        
        p_headers[v_i] := { c_header_host, { PICS_HEADER_HOST } };
        v_i := v_i + 1;
        p_headers[v_i] := { c_header_content_type, { p_header_content_type } };
        v_i := v_i + 1;
        if (p_header_content_text != "") {
          p_headers[v_i] := { c_header_content_text, { p_header_content_text } };
          v_i := v_i + 1;
        }
        p_headers[v_i] := { c_header_content_length, { "0" } };
        v_i := v_i + 1;
        p_headers[v_i] := { c_header_connection, { "keep-alive" } };
        v_i := v_i + 1;
        p_headers[v_i] := { c_header_pragma, { "no-cache" } };
        v_i := v_i + 1;
        p_headers[v_i] := { c_header_cache_control, { "no-cache" } };
YannGarcia's avatar
YannGarcia committed
69
        v_i := v_i + 1;
YannGarcia's avatar
YannGarcia committed
70
71
72
73
        if (PICS_USE_TOKEN_HEADER) {
          p_headers[v_i] := { c_header_authorization, { PICS_TOKEN_HEADER } }; // aladdin:opensesame
          v_i := v_i + 1;
        }
Yann Garcia's avatar
Yann Garcia committed
74
        //p_headers[v_i] := { c_header_accept, { "application/x-its-response" } };
75
76
77
        if (lengthof(PX_ADDITIONAL_HTTP_HEADERS_KEYS) != 0) {
          f_set_headers_list(PX_ADDITIONAL_HTTP_HEADERS_KEYS, PX_ADDITIONAL_HTTP_HEADERS_VALUES, p_headers);
        }
Yann Garcia's avatar
Yann Garcia committed
78
79
      } // End of function f_init_default_headers_list
      
Yann Garcia's avatar
Yann Garcia committed
80
81
82
      function f_set_headers_list(
                                  in charstring_list p_headers_to_set,
                                  in charstring_list p_headers_value,
Yann Garcia's avatar
Yann Garcia committed
83
                                  inout Headers p_headers
Yann Garcia's avatar
Yann Garcia committed
84
                                  ) {
85
86
87
88
89
90
91
92
93
94
95
        // Sanity checks
        if (lengthof(p_headers_to_set) == 0) {
          return;
        } else if (lengthof(p_headers) == 0) {
          return;
        }
        
        for (var integer v_idx := 0; v_idx < lengthof(p_headers_to_set); v_idx := v_idx + 1) {
          var integer v_jdx;
          for (v_jdx := 0; v_jdx < lengthof(p_headers); v_jdx := v_jdx + 1) {
            if (p_headers[v_jdx].header_name == p_headers_to_set[v_idx]) {
96
              p_headers[v_jdx].header_value := { p_headers_value[v_idx] }; // NOTE Codec won't encode it
97
98
99
              break;
            }
          } // End of 'for' statement 
Yann Garcia's avatar
Yann Garcia committed
100
101
102
103
          if (v_jdx == lengthof(p_headers)) {
            p_headers[v_jdx].header_name := p_headers_to_set[v_idx];
            p_headers[v_jdx].header_value := { p_headers_value[v_idx] };
          }
104
        } // End of 'for' statement
Yann Garcia's avatar
Yann Garcia committed
105
      } // End of function f_set_headers_list
106
      
Yann Garcia's avatar
Yann Garcia committed
107
108
      function f_remove_headers_list(
                                     in charstring_list p_headers_to_remove,
Yann Garcia's avatar
Yann Garcia committed
109
                                     inout Headers p_headers
Yann Garcia's avatar
Yann Garcia committed
110
                                     ) {
111
112
113
114
115
116
117
118
119
120
121
122
123
        // Sanity checks
        if (lengthof(p_headers_to_remove) == 0) {
          return;
        } else if (lengthof(p_headers) == 0) {
          return;
        }
        
        for (var integer v_idx := 0; v_idx < lengthof(p_headers_to_remove); v_idx := v_idx + 1) {
          for (var integer v_jdx := 0; v_jdx < lengthof(p_headers); v_jdx := v_jdx + 1) {
            if (p_headers[v_jdx].header_name == p_headers_to_remove[v_idx]) {
              p_headers[v_jdx].header_value := omit; // NOTE Codec won't encode it
              break;
            }
Yann Garcia's avatar
Yann Garcia committed
124
          } // End of 'for' statement
125
        } // End of 'for' statement
Yann Garcia's avatar
Yann Garcia committed
126
127
128
      } // End of function f_remove_headers_list
      
      function f_get_header(
Yann Garcia's avatar
Yann Garcia committed
129
                            in Headers p_headers,
Yann Garcia's avatar
Yann Garcia committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
                            in charstring p_header_name := c_header_content_text,
                            out charstring_list p_header_value
                                      ) {
        // Sanity checks
        if (lengthof(p_header_name) == 0) {
          return;
        } else if (lengthof(p_headers) == 0) {
          return;
        }
        
        for (var integer v_jdx := 0; v_jdx < lengthof(p_headers); v_jdx := v_jdx + 1) {
          if (p_headers[v_jdx].header_name == p_header_name) {
            p_header_value := p_headers[v_jdx].header_value; // NOTE Codec won't encode it
            break;
          }
        } // End of 'for' statement
      } // End of function f_get_header
Yann Garcia's avatar
Yann Garcia committed
147
      
YannGarcia's avatar
YannGarcia committed
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
      /**
      * @desc Check HTTP response headers
      * @param p_headers The HTTP headers
      * @param p_value the expected va;ue
      * @return true on success, false otherwise
      */
      function f_check_headers(
                               in Headers p_headers,
                               in charstring p_header_name := "Location",
                               in template (present) charstring p_value := ?
                               ) return boolean {
        // Local variables
        var boolean v_header_matched := false;
        
        for (var integer v_idx := 0; v_idx < lengthof(p_headers); v_idx := v_idx + 1) {
          if (p_headers[v_idx].header_name == p_header_name) {
            if (match(p_headers[v_idx].header_value[0], p_value) == true) { 
              v_header_matched := true;
            } // else, nothing to do
            break;
          }
        } // End of 'for' statement
        
        return v_header_matched;
      } // End of function f_check_headers

Yann Garcia's avatar
Yann Garcia committed
174
175
    } // End of group http_headers
    
garciay's avatar
garciay committed
176
} // End of module LibItsHttp_Functions