Opens a socket and makes a request to the url of choice. Returns an
object with statusCode, status, content and the received headers.
559335
private function httpRequest(
        $url,
        $request,
        $data = array(),
        $sendHeaders = array(),
        $timeout = 10
    ) {
        $streamParams = array(
            'http' => array(
                'method' => $request,
                'ignore_errors' => true,
                'timeout' => $timeout,
                'header' => "User-Agent: " . $this->userAgent . "\r\n"
            )
        );
        foreach ($sendHeaders as $header => $value) {
            $streamParams['http']['header'] .= $header . ': ' . $value . "\r\n";
        }
        foreach ($this->extraHeaders as $header => $value) {
            $streamParams['http']['header'] .= $header . ': ' . $value . "\r\n";
        }

        if (is_array($data)) {
            if (in_array($request, array('POST', 'PUT')))
                $streamParams['http']['content'] = http_build_query($data);
            elseif ($data && count($data))
                $url .= '?' . http_build_query($data);
        } elseif ($data !== null) {
            if (in_array($request, array('POST', 'PUT')))
                $streamParams['http']['content'] = $data;
        }

        $streamParams['cURL'] = $streamParams['http'];

        $context = stream_context_create($streamParams);
        $fp = @fopen($url, 'rb', false, $context);
        if (!$fp)
            $content = false;
        else
            $content = stream_get_contents($fp);

        $status = null;
        $statusCode = null;
        $headers = array();

        if ($content !== false) {
            $metadata = stream_get_meta_data($fp);
            if (isset($metadata['wrapper_data']['headers']))
                $headerMetadata = $metadata['wrapper_data']['headers'];
            else
                $headerMetadata = $metadata['wrapper_data'];
            foreach ($headerMetadata as $row) {
                if (preg_match('/^HTTP\/1\.[01] (\d{3})\s*(.*)/', $row, $m)) {
                    $statusCode = $m[1];
                    $status = $m[2];
                } elseif (preg_match('/^([^:]+):\s*(.*)$/', $row, $m)) {
                    $headers[mb_strtolower($m[1])] = $m[2];
                }
            }
        }

        return (object) array(
            'ok' => $statusCode == 200,
            'statusCode' => $statusCode,
            'status' => $status,
            'content' => $content,
            'headers' => $headers
        );
    }
963540
function getProtocol(req) {
	if (req.raw.socket.encrypted) return 'https';
	const proto = req['X-Forwarded-Proto'] || '';
	return proto ? proto.split(/\s*,\s*/)[0] : 'http';
}
267755
def http_stream(url, chunk=4096):
    
    ret = requests.get(url, stream=True)
    ret.raise_for_status()
    for data in ret.iter_content(chunk):
        yield data
652102
def _grabContentFromUrl(self, url):
        
        # Defining an empty object for the response
        response = {}
                
        # This part has to be modified...        
        try:            
            # Configuring the socket
            socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, self.info["host"], int(self.info["port"]), True)
            s = socks.socksocket()
            
            # Extracting the domain from the URL
            domain = self.getDomainFromUrl(url)        
            s.connect((domain, 80))

            message = 'GET ' + url + ' HTTP/1.0\r\n\r\n'
            s.sendall(message)
            
            data = ""
            while True:
                reply = s.recv(4096)

                if not reply:
                    break    
                else:
                    data += reply    
            
            # Processing data as expected
            response = self._createDataStructure(data)     
            
        # Try to make the errors clear for other users
        except socks.ProxyConnectionError, sPCE:
            errMsg = "ERROR socks.ProxyConnectionError. Something seems to be wrong with the Tor Bundler."   
            raise Exception( errMsg + " " + str(sPCE))        
            
        return response
117870
def open(self, fullurl, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
        
        if isinstance(fullurl, bytes):
            fullurl = fullurl.decode()
        if isinstance(fullurl, str):
            req = Request(fullurl, data)
        else:
            req = fullurl
            if data is not None:
                req.data = data

        req.timeout = timeout
        protocol = req.type

        # pre-process request
        meth_name = protocol+"_request"
        for processor in self.process_request.get(protocol, []):
            meth = getattr(processor, meth_name)
            req = meth(req)

        response = self._open(req, data)

        # post-process response
        meth_name = protocol+"_response"
        for processor in self.process_response.get(protocol, []):
            meth = getattr(processor, meth_name)
            response = meth(req, response)

        return response
529653
def _send_http_request(self, xml_request):
        
        headers = {"Host": self._host, "Content-Type": "text/xml", "Recipient": self._storage}
        try: # Retry once if failed in case the socket has just gone bad.
            self._connection.request("POST", self._selector_url, xml_request, headers)
            response = self._connection.getresponse()
        except (httplib.CannotSendRequest, httplib.BadStatusLine):
            Debug.warn("\nRestarting socket, resending message!")
            self._open_connection()
            self._connection.request("POST", self._selector_url, xml_request, headers)
            response = self._connection.getresponse()
        data = response.read()
        return data
491039
def rest_get_stream(self, url, auth=None, verify=True, cert=None):
        
        res = requests.get(url, auth=auth, stream=True, verify=verify, cert=cert)
        return res.raw, res.status_code
305731
def request(self, url, extra_headers=None, params=None):
        ''
        if not url.startswith('http'):
            # relative url
            url = self.rootpath + url
        log.debug("getting url: %r, extra_headers=%r, params=%r", url, extra_headers, params)
        if extra_headers is None: extra_headers={}
        r = self.session.get(url, headers=extra_headers, params=params, timeout=1800) #max retries is set in __init__

        if r.status_code in ( 500, ):
            raise JFSError(r.reason)
        return r
365011
def postURL(self, url, headers={}, body=None):
        
        return self._load_resource("POST", url, headers, body)
203616
function open() {
    var what
      , slice = true
      , requests = this
      , socket = requests.socket;

    requests.on('stream', function stream(data) {
      if (!slice) {
        return requests.emit('data', data);
      }

      //
      // Please note that we need to use a method here that works on both string
      // as well as ArrayBuffer's as we have no certainty that we're receiving
      // text.
      //
      var chunk = data.slice(requests.offset);
      requests.offset = data.length;

      requests.emit('data', chunk);
    });

    requests.on('end', function cleanup() {
      delete Requests.active[requests.id];
    });

    if (this.timeout) {
      socket.timeout = +this.timeout;
    }

    if ('cors' === this.mode.toLowerCase() && 'withCredentials' in socket) {
      socket.withCredentials = true;
    }

    //
    // ActiveXObject will throw an `Type Mismatch` exception when setting the to
    // an null-value and to be consistent with all XHR implementations we're going
    // to cast the value to a string.
    //
    // While we don't technically support the XDomainRequest of IE, we do want to
    // double check that the setRequestHeader is available before adding headers.
    //
    // Chrome has a bug where it will actually append values to the header instead
    // of overriding it. So if you do a double setRequestHeader(Content-Type) with
    // text/plain and with text/plain again, it will end up as `text/plain,
    // text/plain` as header value. This is why use a headers object as it
    // already eliminates duplicate headers.
    //
    for (what in this.headers) {
      if (this.headers[what] !== undefined && this.socket.setRequestHeader) {
        this.socket.setRequestHeader(what, this.headers[what] +'');
      }
    }

    //
    // Set the correct responseType method.
    //
    if (requests.streaming) {
      if (!this.body || 'string' === typeof this.body) {
        if ('multipart' in socket) {
          socket.multipart = true;
          slice = false;
        } else if (Requests.type.mozchunkedtext) {
          socket.responseType = 'moz-chunked-text';
          slice = false;
        }
      } else {
        if (Requests.type.mozchunkedarraybuffer) {
          socket.responseType = 'moz-chunked-arraybuffer';
        } else if (Requests.type.msstream) {
          socket.responseType = 'ms-stream';
        }
      }
    }

    listeners(socket, requests, requests.streaming);
    requests.emit('before', socket);

    send(socket, this.body, hang(function send(err) {
      if (err) {
        requests.emit('error', err);
        requests.emit('end', err);
      }

      requests.emit('send');
    }));
  }
343089
def communicate(self):
        """"""
        request_seen = False
        try:
            while True:
                # (re)set req to None so that if something goes wrong in
                # the RequestHandlerClass constructor, the error doesn't
                # get written to the previous request.
                req = None
                req = self.RequestHandlerClass(self.server, self)

                # This order of operations should guarantee correct pipelining.
                req.parse_request()
                if self.server.stats['Enabled']:
                    self.requests_seen += 1
                if not req.ready:
                    # Something went wrong in the parsing (and the server has
                    # probably already made a simple_response). Return and
                    # let the conn close.
                    return

                request_seen = True
                req.respond()
                if req.close_connection:
                    return
        except socket.error as ex:
            errnum = ex.args[0]
            # sadly SSL sockets return a different (longer) time out string
            timeout_errs = 'timed out', 'The read operation timed out'
            if errnum in timeout_errs:
                # Don't error if we're between requests; only error
                # if 1) no request has been started at all, or 2) we're
                # in the middle of a request.
                # See https://github.com/cherrypy/cherrypy/issues/853
                if (not request_seen) or (req and req.started_request):
                    self._conditional_error(req, '408 Request Timeout')
            elif errnum not in errors.socket_errors_to_ignore:
                self.server.error_log(
                    'socket.error %s' % repr(errnum),
                    level=logging.WARNING, traceback=True,
                )
                self._conditional_error(req, '500 Internal Server Error')
        except (KeyboardInterrupt, SystemExit):
            raise
        except errors.FatalSSLAlert:
            pass
        except errors.NoSSLError:
            self._handle_no_ssl(req)
        except Exception as ex:
            self.server.error_log(
                repr(ex), level=logging.ERROR, traceback=True,
            )
            self._conditional_error(req, '500 Internal Server Error')
831977
def send(self, request):
        
        self._logger.debug('Opening connection')
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.connect((self._host, self._port))
        talk = SocketTalk(sock, encode=self._encode)

        self._logger.debug('Sending action %s' % request.action)
        if not talk.put(request.action):
            self._logger.error('Failed to send action %s' % request.action)
            return None

        if request.key is not None:
            self._logger.debug('Sending key %s' % request.key)
            if not talk.put(request.key):
                self._logger.error('Failed to send key %s' % request.key)
                return None

        if request.value is not None:
            self._logger.debug('Sending value')
            if not talk.put(request.value):
                self._logger.error('Failed to send value')
                return None

        self._logger.debug('Receiving status')
        response = talk.get()
        if not response:
            self._logger.error('Failed to receive status')
            return None
        self._logger.debug('Status response = %s' % response)

        if request.action in ('get', 'size', 'keys') and response == 'ok':
            self._logger.debug('Receiving value')
            response = talk.get()
            if not response:
                self._logger.error('Failed to receive value')
                return None

        if request.action in ('keys',):
            response = pickle.loads(response)

        self._logger.debug('Closing connection')
        try:
            sock.shutdown(socket.SHUT_RDWR)
        except:
            self._logger.error('Failed to shutdown')
        sock.close()

        return response
889939
def _request(self, url, method='GET', params=None):
        """"""
        self.connected = True
        retry_counter = 0

        method = method.lower()
        func = getattr(self.client, method)
        params, _ = _transparent_params(params)

        def _send(retry_counter):
            requests_args = {}
            for k, v in self.client_args.items():
                # Maybe this should be set as a class
                # variable and only done once?
                if k in ('timeout', 'allow_redirects', 'verify'):
                    requests_args[k] = v

            while self.connected:
                try:
                    if method == 'get':
                        requests_args['params'] = params
                    else:
                        requests_args['data'] = params

                    response = func(url, **requests_args)
                except requests.exceptions.Timeout:
                    self.on_timeout()
                else:
                    if response.status_code != 200:
                        self.on_error(response.status_code, response.content)

                    if self.retry_count and \
                       (self.retry_count - retry_counter) > 0:
                        time.sleep(self.retry_in)
                        retry_counter += 1
                        _send(retry_counter)

                    return response

        while self.connected:
            response = _send(retry_counter)

            for line in response.iter_lines(self.chunk_size):
                if not self.connected:
                    break
                if line:
                    try:
                        if is_py3:
                            line = line.decode('utf-8')
                        data = json.loads(line)
                    except ValueError:  # pragma: no cover
                        self.on_error(response.status_code,
                                      'Unable to decode response, \
                                      not valid JSON.')
                    else:
                        if self.on_success(data):  # pragma: no cover
                            for message_type in self.handlers:
                                if message_type in data:
                                    handler = getattr(self,
                                                      'on_' + message_type,
                                                      None)
                                    if handler \
                                       and callable(handler) \
                                       and not handler(data.get(message_type)):
                                        break

        response.close()
458789
def _fetch_socket_data(self, parsed_url):
        '''  '''

        self.log.debug("Fetching haproxy stats from socket: %s" % parsed_url.geturl())

        if parsed_url.scheme == 'tcp':
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            splitted_loc = parsed_url.netloc.split(':')
            host = splitted_loc[0]
            port = int(splitted_loc[1])
            sock.connect((host, port))
        else:
            sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
            sock.connect(parsed_url.path)
        sock.send(b"show stat\r\n")

        response = ""
        output = sock.recv(BUFSIZE)
        while output:
            response += output.decode("ASCII")
            output = sock.recv(BUFSIZE)

        sock.close()

        return response.splitlines()
427800
def _get_http_request(netloc, path="/", headers=None, ssl=False):
    
    if ssl:
        port = 443
    else:
        port = 80

    host = netloc

    if len(netloc.split(":")) == 2:
        host, port = netloc.split(":")

    request = {"host": host,
               "port": port,
               "path": path,
               "ssl": ssl,
               "method": "GET"}
    if headers:
        request["headers"] = headers

    response = {}

    try:
        conn = ICHTTPConnection(host=host, port=port, timeout=10)

        conn.request(path, headers, ssl, timeout=10)
        response["status"] = conn.status
        response["reason"] = conn.reason
        response["headers"] = conn.headers
        body = conn.body

        try:
            response["body"] = body.encode('utf-8')
        except UnicodeDecodeError:
            # if utf-8 fails to encode, just use base64
            response["body.b64"] = body.encode('base64')

    except Exception as err:
        response["failure"] = str(err)

    result = {"response": response,
              "request": request}

    return result
673708
def _grabContentFromUrl(self, url):
        
        # Defining an empty object for the response
        info = {}
                
        # This part has to be modified...        
        try:            
            # Configuring the socket
            queryURL = "http://" + self.info["host"] + ":" + self.info["port"] + "/" + url
            response = urllib2.urlopen(queryURL)

            # Rebuilding data to be processed
            data = str(response.headers) + "\n"

            data += response.read()

            # Processing data as expected
            info = self._createDataStructure(data)     
            
        # Try to make the errors clear for other users
        except Exception, e:
            errMsg = "ERROR Exception. Something seems to be wrong with the Zeronet Bundler."   
            raise Exception( errMsg + " " + str(e))        
            
        return info
781189
def request(url, message):
    """"""
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    host, port = url.split(':')
    port = int(port)
    s.connect((host, port))
    s.send(pack(message))
    data = s.makefile().read()
    s.close()
    return data