All files / src/http zcl_abapgit_http.clas.abap

100% Statements 378/378
100% Branches 0/0
100% Functions 0/0
100% Lines 378/378

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 3791x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x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TS:
      BEGIN OF c_scheme,
        digest TYPE string VALUE 'Digest',
      END OF c_scheme .
 
    CLASS-METHODS get_agent
      RETURNING
        VALUE(rv_agent) TYPE string .
 
    TYPES: BEGIN OF ty_key_value,
             key   TYPE string,
             value TYPE string,
           END OF ty_key_value.
    TYPES ty_headers TYPE STANDARD TABLE OF ty_key_value WITH DEFAULT KEY.
 
    CLASS-METHODS create_by_url
      IMPORTING
        !iv_url          TYPE string
        !iv_service      TYPE string
        it_headers       TYPE ty_headers OPTIONAL
      RETURNING
        VALUE(ro_client) TYPE REF TO zcl_abapgit_http_client
      RAISING
        zcx_abapgit_exception .
 
    CLASS-METHODS check_connection
      IMPORTING
        !iv_url TYPE string
      RAISING
        zcx_abapgit_exception.
  PROTECTED SECTION.
 
    CLASS-METHODS check_auth_requested
      IMPORTING
        !ii_client               TYPE REF TO if_http_client
      RETURNING
        VALUE(rv_auth_requested) TYPE abap_bool
      RAISING
        zcx_abapgit_exception .
    CLASS-METHODS is_local_system
      IMPORTING
        !iv_url        TYPE string
      RETURNING
        VALUE(rv_bool) TYPE abap_bool .
    CLASS-METHODS acquire_login_details
      IMPORTING
        !ii_client       TYPE REF TO if_http_client
        !io_client       TYPE REF TO zcl_abapgit_http_client
        !iv_url          TYPE string
      RETURNING
        VALUE(rv_scheme) TYPE string
      RAISING
        zcx_abapgit_exception .
 
    CLASS-METHODS get_http_client
      IMPORTING
        !iv_url          TYPE string
      RETURNING
        VALUE(ri_client) TYPE REF TO if_http_client
      RAISING
        zcx_abapgit_exception.
 
    CLASS-METHODS get_connection_longtext
      IMPORTING
        !iv_host           TYPE string
        !iv_ssl_id         TYPE ssfapplssl
        !iv_proxy_host     TYPE string
        !iv_proxy_service  TYPE string
      RETURNING
        VALUE(rv_longtext) TYPE string.
 
  PRIVATE SECTION.
ENDCLASS.
 
 
 
CLASS zcl_abapgit_http IMPLEMENTATION.
 
 
  METHOD acquire_login_details.
 
    DATA: lv_default_user TYPE string,
          lv_user         TYPE string,
          lv_pass         TYPE string,
          lo_digest       TYPE REF TO zcl_abapgit_http_digest.
 
 
    lv_default_user = zcl_abapgit_persistence_user=>get_instance( )->get_repo_login( iv_url ).
    lv_user         = lv_default_user.
 
    zcl_abapgit_password_dialog=>popup(
      EXPORTING
        iv_repo_url     = iv_url
      CHANGING
        cv_user         = lv_user
        cv_pass         = lv_pass ).
 
    IF lv_user IS INITIAL.
      zcx_abapgit_exception=>raise( 'Unauthorized access. Check your credentials' ).
    ENDIF.
 
    IF lv_user <> lv_default_user.
      zcl_abapgit_persistence_user=>get_instance( )->set_repo_login(
        iv_url   = iv_url
        iv_login = lv_user ).
    ENDIF.
 
    rv_scheme = ii_client->response->get_header_field( 'www-authenticate' ).
    FIND REGEX '^(\w+)' IN rv_scheme SUBMATCHES rv_scheme.
 
    CASE rv_scheme.
      WHEN c_scheme-digest.
* https://en.wikipedia.org/wiki/Digest_access_authentication
* e.g. used by https://www.gerritcodereview.com/
        CREATE OBJECT lo_digest
          EXPORTING
            ii_client   = ii_client
            iv_username = lv_user
            iv_password = lv_pass.
        lo_digest->run( ii_client ).
        io_client->set_digest( lo_digest ).
      WHEN OTHERS.
* https://en.wikipedia.org/wiki/Basic_access_authentication
        ii_client->authenticate(
          username = lv_user
          password = lv_pass ).
    ENDCASE.
 
  ENDMETHOD.
 
 
  METHOD check_auth_requested.
 
    DATA: lv_code TYPE i.
 
    ii_client->response->get_status( IMPORTING code = lv_code ).
    IF lv_code = 401.
      rv_auth_requested = abap_true.
    ENDIF.
 
  ENDMETHOD.
 
 
  METHOD check_connection.
    " Check if a connection from this system to the git host is possible
    " This will validate the general HTTP/HTTPS/SSL configuration and certificates
    get_http_client( iv_url ).
  ENDMETHOD.
 
 
  METHOD create_by_url.
 
    DATA: lv_uri           TYPE string,
          lv_scheme        TYPE string,
          lv_authorization TYPE string,
          li_client        TYPE REF TO if_http_client,
          ls_header        LIKE LINE OF it_headers.
 
    li_client = get_http_client( iv_url ).
 
    CREATE OBJECT ro_client
      EXPORTING
        ii_client = li_client.
 
    IF is_local_system( iv_url ) = abap_true.
      li_client->send_sap_logon_ticket( ).
    ENDIF.
 
    li_client->request->set_cdata( '' ).
    li_client->request->set_header_field(
        name  = '~request_method'
        value = 'GET' ).
    li_client->request->set_header_field(
        name  = 'user-agent'
        value = get_agent( ) ).
    lv_uri = zcl_abapgit_url=>path_name( iv_url ) &&
             '/info/refs?service=git-' &&
             iv_service &&
             '-pack'.
    li_client->request->set_header_field(
        name  = '~request_uri'
        value = lv_uri ).
 
    LOOP AT it_headers INTO ls_header.
      li_client->request->set_header_field(
        name  = ls_header-key
        value = ls_header-value ).
    ENDLOOP.
 
    " Disable internal auth dialog (due to its unclarity)
    li_client->propertytype_logon_popup = if_http_client=>co_disabled.
 
    lv_authorization = zcl_abapgit_login_manager=>load( iv_url ).
    IF lv_authorization IS NOT INITIAL.
      li_client->request->set_header_field(
        name  = 'authorization'
        value = lv_authorization ).
      li_client->propertytype_logon_popup = li_client->co_disabled.
    ENDIF.
 
    li_client->request->set_version( if_http_request=>co_protocol_version_1_1 ).
 
    zcl_abapgit_exit=>get_instance( )->http_client(
      iv_url    = iv_url
      ii_client = li_client ).
 
    ro_client->send_receive( ).
    IF check_auth_requested( li_client ) = abap_true.
      lv_scheme = acquire_login_details( ii_client = li_client
                                         io_client = ro_client
                                         iv_url    = iv_url ).
      ro_client->send_receive( ).
    ENDIF.
    ro_client->check_http_200( ).
 
    IF lv_scheme <> c_scheme-digest.
      zcl_abapgit_login_manager=>save(
        iv_uri           = iv_url
        iv_authorization = li_client->request->get_header_field( 'authorization' ) ).
    ENDIF.
 
  ENDMETHOD.
 
 
  METHOD get_agent.
 
* bitbucket require agent prefix = "git/"
* also see https://github.com/abapGit/abapGit/issues/1432
    rv_agent = |git/2.0 (abapGit { zif_abapgit_version=>c_abap_version })|.
 
  ENDMETHOD.
 
 
  METHOD get_connection_longtext.
 
    CONSTANTS lc_docs TYPE string VALUE 'https://docs.abapgit.org/user-guide/setup/ssl-setup.html'.
 
    DATA lv_proxy TYPE string.
 
    IF iv_proxy_host IS NOT INITIAL.
      lv_proxy = | via proxy <b>{ iv_proxy_host }:{ iv_proxy_service }</b>|.
    ENDIF.
 
    rv_longtext = |abapGit is trying to connect to <b>{ iv_host }</b> |
      && |using SSL certificates under <b>{ iv_ssl_id }</b>{ lv_proxy }. |
      && |Check system parameters (transaction |
      && zcl_abapgit_html=>create( )->a(
        iv_txt   = 'RZ10'
        iv_act   = |{ zif_abapgit_definitions=>c_action-jump_transaction }?transaction=RZ10|
        iv_class = 'no-pad' )
      && |), SSL setup (transaction |
      && zcl_abapgit_html=>create( )->a(
        iv_txt   = 'STRUST'
        iv_act   = |{ zif_abapgit_definitions=>c_action-jump_transaction }?transaction=STRUST|
        iv_class = 'no-pad' )
      && |), Internet connection monitor (transaction |
      && zcl_abapgit_html=>create( )->a(
        iv_txt   = 'SMICM'
        iv_act   = |{ zif_abapgit_definitions=>c_action-jump_transaction }?transaction=SMICM|
        iv_class = 'no-pad' )
      && |)|.
 
    IF lv_proxy IS NOT INITIAL.
      rv_longtext = rv_longtext
        && |, and proxy configuration (|
        && zcl_abapgit_html=>create( )->a(
          iv_txt   = 'global settings'
          iv_act   = |{ zif_abapgit_definitions=>c_action-go_settings }|
          iv_class = 'no-pad' )
        && |)|.
    ENDIF.
 
    rv_longtext = rv_longtext
      && |. It's recommended to get your SAP Basis and network teams involved. |
      && |For more information and troubleshooting, see the |
      && zcl_abapgit_html=>create( )->a(
        iv_txt   = 'abapGit documentation'
        iv_act   = |{ zif_abapgit_definitions=>c_action-url }?url={ lc_docs }|
        iv_class = 'no-pad' )
      && |.|.
 
  ENDMETHOD.
 
 
  METHOD get_http_client.
 
    DATA:
      lv_error               TYPE string,
      lv_longtext            TYPE string,
      lv_host                TYPE string,
      lv_ssl_id              TYPE ssfapplssl,
      lv_proxy_host          TYPE string,
      lv_proxy_service       TYPE string,
      lo_proxy_configuration TYPE REF TO zcl_abapgit_proxy_config.
 
    CREATE OBJECT lo_proxy_configuration.
 
    ri_client = zcl_abapgit_exit=>get_instance( )->create_http_client( iv_url ).
 
    IF ri_client IS INITIAL.
 
      lv_host          = zcl_abapgit_url=>host( iv_url ).
      lv_ssl_id        = zcl_abapgit_exit=>get_instance( )->get_ssl_id( ).
      lv_proxy_host    = lo_proxy_configuration->get_proxy_url( iv_url ).
      lv_proxy_service = lo_proxy_configuration->get_proxy_port( iv_url ).
 
      lv_longtext = get_connection_longtext(
        iv_host          = lv_host
        iv_ssl_id        = lv_ssl_id
        iv_proxy_host    = lv_proxy_host
        iv_proxy_service = lv_proxy_service ).
 
      cl_http_client=>create_by_url(
        EXPORTING
          url                = lv_host
          ssl_id             = lv_ssl_id
          proxy_host         = lv_proxy_host
          proxy_service      = lv_proxy_service
        IMPORTING
          client             = ri_client
        EXCEPTIONS
          argument_not_found = 1
          plugin_not_active  = 2
          internal_error     = 3
          OTHERS             = 4 ).
      IF sy-subrc <> 0.
        CASE sy-subrc.
          WHEN 1.
            lv_error = 'ARGUMENT_NOT_FOUND'.
          WHEN 2.
            lv_error = 'PLUGIN_NOT_ACTIVE'.
          WHEN 3.
            lv_error = 'INTERNAL_ERROR'.
          WHEN OTHERS.
            lv_error = |OTHER_ERROR_{ sy-subrc }|.
        ENDCASE.
        zcx_abapgit_exception=>raise(
          iv_text     = |Error { lv_error } creating HTTP connection. Check the configuration|
          iv_longtext = lv_longtext ).
      ENDIF.
 
    ENDIF.
 
    IF lo_proxy_configuration->get_proxy_authentication( iv_url ) = abap_true.
      zcl_abapgit_proxy_auth=>run( ri_client ).
    ENDIF.
 
  ENDMETHOD.
 
 
  METHOD is_local_system.
 
    DATA: lv_host TYPE string,
          lt_list TYPE zif_abapgit_definitions=>ty_string_tt,
          li_exit TYPE REF TO zif_abapgit_exit.
 
 
    cl_http_server=>get_location( IMPORTING host = lv_host ).
    APPEND lv_host TO lt_list.
 
    APPEND 'localhost' TO lt_list.
 
    li_exit = zcl_abapgit_exit=>get_instance( ).
    li_exit->change_local_host( CHANGING ct_hosts = lt_list ).
 
    FIND REGEX 'https?://([^/^:]*)' IN iv_url SUBMATCHES lv_host.
 
    READ TABLE lt_list WITH KEY table_line = lv_host TRANSPORTING NO FIELDS.
    rv_bool = boolc( sy-subrc = 0 ).
 
  ENDMETHOD.
ENDCLASS.