| 1 | #/usr/bin/env perl | ||
| 2 | ############################################################################### | ||
| 3 | # | ||
| 4 | # @file Request.pm | ||
| 5 | # | ||
| 6 | # @brief API::Eulerian::EDW Request module used to send HTTP request to remote Peer. | ||
| 7 | # | ||
| 8 | # @author Thorillon Xavier:x.thorillon@eulerian.com | ||
| 9 | # | ||
| 10 | # @date 26/11/2021 | ||
| 11 | # | ||
| 12 | # @version 1.0 | ||
| 13 | # | ||
| 14 | ############################################################################### | ||
| 15 | # | ||
| 16 | # Setup module name. | ||
| 17 | # | ||
| 18 | package API::Eulerian::EDW::Request; | ||
| 19 | # | ||
| 20 | # Enforce compilor rules | ||
| 21 | # | ||
| 22 | use strict; use warnings; | ||
| 23 | # | ||
| 24 | # Import API::Eulerian::EDW::Status | ||
| 25 | # | ||
| 26 | use API::Eulerian::EDW::Status; | ||
| 27 | # | ||
| 28 | # Import HTTP::Headers | ||
| 29 | # | ||
| 30 | use HTTP::Headers; | ||
| 31 | # | ||
| 32 | # Import HTTP::Request | ||
| 33 | # | ||
| 34 | use HTTP::Request; | ||
| 35 | # | ||
| 36 | # Import LWP::UserAgent | ||
| 37 | # | ||
| 38 | use LWP::UserAgent; | ||
| 39 | # | ||
| 40 | # Import IO::Socket::SSL | ||
| 41 | # | ||
| 42 | use IO::Socket::SSL; | ||
| 43 | # | ||
| 44 | # Import HTTP::Status | ||
| 45 | # | ||
| 46 | use HTTP::Status qw( :constants :is status_message ); | ||
| 47 | # | ||
| 48 | # Import JSON | ||
| 49 | # | ||
| 50 | use JSON; | ||
| 51 | # | ||
| 52 | # Import Encode | ||
| 53 | # | ||
| 54 | use Encode; | ||
| 55 | # | ||
| 56 | # @brief Create new HTTP Headers. | ||
| 57 | # | ||
| 58 | # @param $class - API::Eulerian::EDW::HTTP class. | ||
| 59 | # | ||
| 60 | # @return HTTP Headers. | ||
| 61 | # | ||
| 62 | sub headers | ||
| 63 | { | ||
| 64 | return HTTP::Headers->new(); | ||
| 65 | } | ||
| 66 | # | ||
| 67 | # @brief Test if the content type of given HTTP response is a | ||
| 68 | # JSON format. | ||
| 69 | # | ||
| 70 | # @param $class - API::Eulerian::EDW::Request Class. | ||
| 71 | # @param $response - HTTP response. | ||
| 72 | # | ||
| 73 | # @return 1 - Content type is JSON. | ||
| 74 | # @return 0 - Content type isnt JSON. | ||
| 75 | # | ||
| 76 | sub is_json | ||
| 77 | { | ||
| 78 | my ( $class, $response ) = @_; | ||
| 79 | my $type; | ||
| 80 | |||
| 81 | # Get content type value from HTTP response | ||
| 82 | $type = $response->header( 'content-type' ); | ||
| 83 | if( defined( $type ) ) { | ||
| 84 | # Split content type into an array. | ||
| 85 | my @subtypes = split( '; ', $type ); | ||
| 86 | # Iterate on subtypes entries | ||
| 87 | foreach my $subtype ( @subtypes ) { | ||
| 88 | # Test if subtype is JSON format | ||
| 89 | if( $subtype eq 'application/json' ) { | ||
| 90 | return 1; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | return 0; | ||
| 96 | } | ||
| 97 | # | ||
| 98 | # @brief Get JSON object from HTTP response. | ||
| 99 | # | ||
| 100 | # @param $class - API::Eulerian::EDW::Request class. | ||
| 101 | # @param $response - HTTP response. | ||
| 102 | # | ||
| 103 | # @return JSON object. | ||
| 104 | # | ||
| 105 | sub json | ||
| 106 | { | ||
| 107 | my ( $class, $response ) = @_; | ||
| 108 | my $data = undef; | ||
| 109 | |||
| 110 | if( $class->is_json( $response ) ) { | ||
| 111 | $data = $response->decoded_content; | ||
| 112 | if( defined( $data ) ) { | ||
| 113 | chomp( $data ); | ||
| 114 | if( length( $data ) > 0 ) { | ||
| 115 | $data = encode( 'utf-8', $data ); | ||
| 116 | $data =~ s/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]//g; | ||
| 117 | $data = decode_json( $data ); | ||
| 118 | } else { | ||
| 119 | $data = undef; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | return $data; | ||
| 125 | } | ||
| 126 | # | ||
| 127 | # @brief Do HTTP Get on given URL. | ||
| 128 | # | ||
| 129 | # @param $class - API::Eulerian::EDW::HTTP class. | ||
| 130 | # @param $url - Remote URL. | ||
| 131 | # @param $headers - HTTP::Headers. | ||
| 132 | # @param $file - Local file path. | ||
| 133 | # | ||
| 134 | # @return API::Eulerian::EDW::Status instance. | ||
| 135 | # | ||
| 136 | sub get | ||
| 137 | { | ||
| 138 | my ( $class, $url, $headers, $file ) = @_; | ||
| 139 | return $class->_request( 'GET', $url, $headers, undef, undef, $file ); | ||
| 140 | } | ||
| 141 | # | ||
| 142 | # @brief Do HTTP Post on given URL. | ||
| 143 | # | ||
| 144 | # @param $class - API::Eulerian::EDW::HTTP class. | ||
| 145 | # @param $url - Remote URL. | ||
| 146 | # @param $headers - HTTP::Headers. | ||
| 147 | # @param $what - Request Data. | ||
| 148 | # @param $type - Request Data Type. | ||
| 149 | # | ||
| 150 | # @return API::Eulerian::EDW::Status instance. | ||
| 151 | # | ||
| 152 | sub post | ||
| 153 | { | ||
| 154 | my ( $class, $url, $headers, $what, $type ) = @_; | ||
| 155 | return $class->_request( 'POST', $url, $headers, $what, $type ); | ||
| 156 | } | ||
| 157 | # | ||
| 158 | # @brief Send HTTP request on given url. | ||
| 159 | # | ||
| 160 | # @param $class - API::Eulerian::EDW Request class. | ||
| 161 | # @param $method - HTTP method. | ||
| 162 | # @param $url - Remote URL. | ||
| 163 | # @param $headers - HTTP headers. | ||
| 164 | # @param $what - Data of POST request. | ||
| 165 | # @param $type - Data type of POST request | ||
| 166 | # @param $file - Local file path used to store HTTP reply. | ||
| 167 | # | ||
| 168 | # @return API::Eulerian::EDW::Status instance. | ||
| 169 | # | ||
| 170 | use Data::Dumper; | ||
| 171 | sub _request | ||
| 172 | { | ||
| 173 | my ( $class, $method, $url, $headers, $what, $type, $file ) = @_; | ||
| 174 | my $status = API::Eulerian::EDW::Status->new(); | ||
| 175 | my $endpoint; | ||
| 176 | my $request; | ||
| 177 | |||
| 178 | # Save url. | ||
| 179 | $status->url( $url ); | ||
| 180 | |||
| 181 | # Ensure default type | ||
| 182 | $type = $type || 'application/json'; | ||
| 183 | |||
| 184 | # Sanity check POST arguments | ||
| 185 | if( $method eq 'POST' ) { | ||
| 186 | if( ! ( defined( $what ) && defined( $type ) ) ) { | ||
| 187 | $status->error( 1 ); | ||
| 188 | $status->msg( "Mandatory argument to post request is/are missing" ); | ||
| 189 | $status->code( 400 ); | ||
| 190 | return $status; | ||
| 191 | } else { | ||
| 192 | # Setup Content_Length and Content_Type | ||
| 193 | $headers->push_header( Content_Length => length( $what ) ); | ||
| 194 | $headers->push_header( Content_Type => $type ); | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | # Create HTTP Request | ||
| 199 | $request = HTTP::Request->new( $method, $url, $headers, $what ); | ||
| 200 | |||
| 201 | # Create End Point used to communicate with remote server | ||
| 202 | $endpoint = LWP::UserAgent->new( | ||
| 203 | keep_alive => 0, | ||
| 204 | cookie_jar => {}, | ||
| 205 | ssl_opts => { | ||
| 206 | SSL_verifycn_publicsuffix => '', | ||
| 207 | SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE, | ||
| 208 | verify_hostname => 0, | ||
| 209 | SSL_hostname => '', | ||
| 210 | }, | ||
| 211 | ); | ||
| 212 | |||
| 213 | # Increase Read Timeout on TCP socket to avoid being disconnected | ||
| 214 | $endpoint->timeout( 1800 ); | ||
| 215 | |||
| 216 | # Send Request, wait response if file is defined reply content is | ||
| 217 | # writen into local file. | ||
| 218 | my $response = $endpoint->request( $request, $file ); | ||
| 219 | my $json = API::Eulerian::EDW::Request->json( $response ); | ||
| 220 | |||
| 221 | $status->{ response } = $response; | ||
| 222 | |||
| 223 | if( $response->code != HTTP_OK ) { | ||
| 224 | $status->error( 1 ); | ||
| 225 | $status->code( $response->code ); | ||
| 226 | $status->msg( | ||
| 227 | defined( $json ) ? | ||
| 228 | encode_json( $json ) : $response->content() | ||
| 229 | ); | ||
| 230 | } else { | ||
| 231 | if( defined( $response->header( 'content-encoding' ) ) ) { | ||
| 232 | $status->{ encoding } = $response->header( 'content-encoding' ); | ||
| 233 | } | ||
| 234 | } | ||
| 235 | |||
| 236 | return $status; | ||
| 237 | } | ||
| 238 | # | ||
| 239 | # End up module properly | ||
| 240 | # | ||
| 241 | 1; | ||
| 242 | |||
| 243 | __END__ |