1
1
module http_header
2
- use iso_c_binding
2
+
3
+ ! ! This module provides a simple key value type to use for HTTP headers.
4
+ ! ! It also provides procedures to inquire about the presence of a key and
5
+ ! ! its value in a header array, as well as a procedure to append new
6
+ ! ! headers to an existing array of headers.
7
+
8
+ use stdlib_ascii, only: to_lower
9
+
3
10
implicit none
4
11
private
12
+
5
13
public :: header_type
14
+ public :: append_header
15
+ public :: get_header_value
16
+ public :: header_has_key
17
+
6
18
type :: header_type
7
19
character (:), allocatable :: key, value
8
20
end type header_type
21
+
22
+ contains
23
+
24
+ subroutine append_header (header , key , value )
25
+ ! ! Append a new header_type instance with key and value members to the
26
+ ! ! header array.
27
+ type (header_type), allocatable , intent (inout ) :: header(:)
28
+ ! ! Header array to append to
29
+ character (* ), intent (in ) :: key
30
+ ! ! Key member of header_type to append
31
+ character (* ), intent (in ) :: value
32
+ ! ! Value member of header_type to append
33
+ type (header_type), allocatable :: temp(:)
34
+ integer :: n
35
+
36
+ if (allocated (header)) then
37
+ n = size (header)
38
+ allocate (temp(n+1 ))
39
+ temp(1 :n) = header
40
+ temp(n+1 ) = header_type(key, value)
41
+ call move_alloc(temp, header)
42
+ else
43
+ allocate (header(1 ))
44
+ header(1 ) = header_type(key, value)
45
+ end if
46
+ end subroutine append_header
47
+
48
+ pure function get_header_value (header , key ) result(val)
49
+ ! ! Return the value of a requested key in a header array. If the key is
50
+ ! ! not found, the function returns an empty string (unallocated). If
51
+ ! ! there are duplicates of the key in the header array, return the value
52
+ ! ! of the first occurence of the key.
53
+ type (header_type), intent (in ) :: header(:)
54
+ ! ! Header to search for key
55
+ character (* ), intent (in ) :: key
56
+ ! ! Key to search in header
57
+ character (:), allocatable :: val
58
+ ! ! Value of the key to return
59
+ integer :: n
60
+
61
+ do n = 1 , size (header)
62
+ if (to_lower(key) == to_lower(header(n)% key)) then
63
+ val = header(n)% value
64
+ return
65
+ end if
66
+ end do
67
+
68
+ end function get_header_value
69
+
70
+ pure logical function header_has_key(header, key)
71
+ ! ! Return .true. if key is present in header, .false. otherwise.
72
+ ! ! HTTP headers are case insensitive, so values are converted to
73
+ ! ! lowercase before comparison.
74
+ type (header_type), intent (in ) :: header(:)
75
+ ! ! Header to search for key
76
+ character (* ), intent (in ) :: key
77
+ ! ! Key to search in header
78
+ integer :: n
79
+
80
+ header_has_key = .false.
81
+ do n = 1 , size (header)
82
+ if (to_lower(key) == to_lower(header(n)% key)) then
83
+ header_has_key = .true.
84
+ return
85
+ end if
86
+ end do
87
+
88
+ end function header_has_key
89
+
9
90
end module http_header
0 commit comments