<commit_before>/**
 * ESP8266HTTPClient.cpp
 *
 * Created on: 02.11.2015
 *
 * Copyright (c) 2015 <NAME>. All rights reserved.
 * This file is part of the ESP8266HTTPClient for Arduino.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <StreamString.h>
#include <base64.h>

#include "ESP8266HTTPClient.h"

/**
 * constractor
 */
HTTPClient::HTTPClient() {
    _tcp = NULL;
    _tcps = NULL;

    _port = 0;

    _reuse = false;
    _tcpTimeout = HTTPCLIENT_DEFAULT_TCP_TIMEOUT;
    _useHTTP10 = false;

    _https = false;

    _userAgent = "ESP8266HTTPClient";

    _headerKeysCount = 0;
    _currentHeaders = NULL;

    _returnCode = 0;
    _size = -1;
    _canReuse = false;
    _transferEncoding = HTTPC_TE_IDENTITY;

}

/**
 * deconstractor
 */
HTTPClient::~HTTPClient() {

    if(_tcps) {
        _tcps->stop();
        delete _tcps;
        _tcps = NULL;
        _tcp = NULL;
    } else if(_tcp) {
        _tcp->stop();
        delete _tcp;
        _tcp = NULL;
    }

    if(_currentHeaders) {
        delete[] _currentHeaders;
    }
}

/**
 * phasing the url for all needed informations
 * @param url const char *
 * @param httpsFingerprint const char *
 */
void HTTPClient::begin(const char *url, const char * httpsFingerprint) {
    begin(String(url), String(httpsFingerprint));
}

/**
 * phasing the url for all needed informations
 * @param url String
 * @param httpsFingerprint String
 */
void HTTPClient::begin(String url, String httpsFingerprint) {

    DEBUG_HTTPCLIENT("[HTTP-Client][begin] url: %s\n", url.c_str());

    _httpsFingerprint = httpsFingerprint;
    _returnCode = 0;
    _size = -1;

    _Headers = "";

    String protocol;
    // check for : (http: or https:
    int index = url.indexOf(':');
    //int index2;
    bool hasPort = false;
    if(index) {
        protocol = url.substring(0, index);
        url.remove(0, (index + 3)); // remove http:// or https://

        index = url.indexOf('/');
        String host = url.substring(0, index);
        url.remove(0, index); // remove host part

        // get Authorization
        index = host.indexOf('@');
        if(index >= 0) {
            // auth info
            String auth = host.substring(0, index);
            host.remove(0, index + 1); // remove auth part including @
            _base64Authorization = base64::encode(auth);
        }

        // get port
        index = host.indexOf(':');
        if(index >= 0) {
            _host = host.substring(0, index); // hostname
            host.remove(0, (index + 1)); // remove hostname + :
            _port = host.toInt(); // get port
            hasPort = true;
        } else {<commit_msg>Fix typo causing crash on missing uri scheme<commit_after>/**
 * ESP8266HTTPClient.cpp
 *
 * Created on: 02.11.2015
 *
 * Copyright (c) 2015 <NAME>. All rights reserved.
 * This file is part of the ESP8266HTTPClient for Arduino.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <StreamString.h>
#include <base64.h>

#include "ESP8266HTTPClient.h"

/**
 * constractor
 */
HTTPClient::HTTPClient() {
    _tcp = NULL;
    _tcps = NULL;

    _port = 0;

    _reuse = false;
    _tcpTimeout = HTTPCLIENT_DEFAULT_TCP_TIMEOUT;
    _useHTTP10 = false;

    _https = false;

    _userAgent = "ESP8266HTTPClient";

    _headerKeysCount = 0;
    _currentHeaders = NULL;

    _returnCode = 0;
    _size = -1;
    _canReuse = false;
    _transferEncoding = HTTPC_TE_IDENTITY;

}

/**
 * deconstractor
 */
HTTPClient::~HTTPClient() {

    if(_tcps) {
        _tcps->stop();
        delete _tcps;
        _tcps = NULL;
        _tcp = NULL;
    } else if(_tcp) {
        _tcp->stop();
        delete _tcp;
        _tcp = NULL;
    }

    if(_currentHeaders) {
        delete[] _currentHeaders;
    }
}

/**
 * phasing the url for all needed informations
 * @param url const char *
 * @param httpsFingerprint const char *
 */
void HTTPClient::begin(const char *url, const char * httpsFingerprint) {
    begin(String(url), String(httpsFingerprint));
}

/**
 * phasing the url for all needed informations
 * @param url String
 * @param httpsFingerprint String
 */
void HTTPClient::begin(String url, String httpsFingerprint) {

    DEBUG_HTTPCLIENT("[HTTP-Client][begin] url: %s\n", url.c_str());

    _httpsFingerprint = httpsFingerprint;
    _returnCode = 0;
    _size = -1;

    _Headers = "";

    String protocol;
    // check for : (http: or https:
    int index = url.indexOf(':');
    //int index2;
    bool hasPort = false;
    if(index >= 0) {
        protocol = url.substring(0, index);
        url.remove(0, (index + 3)); // remove http:// or https://

        index = url.indexOf('/');
        String host = url.substring(0, index);
        url.remove(0, index); // remove host part

        // get Authorization
        index = host.indexOf('@');
        if(index >= 0) {
            // auth info
            String auth = host.substring(0, index);
            host.remove(0, index + 1); // remove auth part including @
            _base64Authorization = base64::encode(auth);
        }

        // get port
        index = host.indexOf(':');
        if(index >= 0) {
            _host = host.substring(0, index); // hostname
            host.remove(0, (index + 1)); // remove hostname + :
            _port = host.toInt(); // get port
            hasPort = true;
        } else {
{'fraction_non_alphanumeric': 0.11598837209302326, 'fraction_numerical': 0.02136627906976744, 'mean_word_length': 2.5396090534979425, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 24, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7585159', 'n_tokens_mistral': 2194, 'n_tokens_neox': 2076, 'n_words': 672}