text:
<?php

namespace UPS;

class Ship {

  private $error;
  private $message;
	private $connector;
	private $xmlSent;
	private $xmlResponse;
	private $dataSent;
	private $dataResponse;

	function __construct($Connector) {
		// Must pass the UPS object to this class for it to work
		$this->connector = $Connector;
	}

	/**
	 * [getShipmentConfirmation description]
	 * @param  array  $request [description]
	 * @return [type]          [description]
	 */
	function getShipmentConfirmation( $data = array() ) {

    /**
     * Merge Data
     */
    $this->dataSent = Common::mergeNested(array(
      'ShipmentConfirmRequest' => array(
        'Request' => array(
          'TransactionReference' => array(
            'CustomerContext' => 'guidlikesubstance',
            'XpciVersion' => '1.0001'
            ),
          'RequestAction' => 'ShipConfirm',
          'RequestOption' => 'nonvalidate',
          ),
        'Shipment' => array(
          'Shipper' => array(
            'Name'          => '',
            'AttentionName' => '',
            'ShipperNumber' => '',
            'Address' => array(
              'AddressLine1'      => '',
              'City'              => '',
              'StateProvinceCode' => '',
              'PostalCode'        => '',
              ),
            ),
          'ShipTo' => array(
            'CompanyName'   => '',
            'AttentionName' => '',
            'PhoneNumber' => array(
              'StructuredPhoneNumber' => array(
                'PhoneDialPlanNumber' => '',
                'PhoneLineNumber'     => '',
                'PhoneExtension'      => '',
                ),
              ),
            'Address' => array(
              'AddressLine1'       => '',
              'City'               => '',
              'StateProvinceCode'  => '',
              'CountryCode'        => '',
              'PostalCode'         => '',
              'ResidentialAddress' => '',
              ),
            ),
          'Service' => array(
            'Code'        => '03',
            'Description' => 'UPS Ground'
            ),
          'PaymentInformation' => array(
            'Prepaid' => array(
              'BillShipper' => array(
                'AccountNumber' => '',
                'CreditCard' => array(
                  'Type'           => '06',
                  'Number'         => '',
                  'ExpirationDate' => '',
                  ),
                ),
              ),
            'BillThirdParty' => array(
							'BillThirdPartyShipper' => array(
								'AccountNumber' => '',
								'ThirdParty' => array(
									'Address' => array(
										'PostalCode' => '',
										'CountryCode' => '',
										),
									),
								),
            	),
          	'FreightCollect' => array(
          		'BillReceiver' => array(
          			'AccountNumber' => '',
          			'Address' => array(
									'PostalCode' => '',
									'CountryCode' => '',
          				),
          			),
          		),
            'ConsigneeBilled' => '',
            ),
          'ShipmentServiceOptions' => array(
            /*
            This is now part of the Pickup API
            'OnCallAir' => array(
              'PickupDetails' => array(
                'PickupDate'        => '',
                'EarliestTimeReady' => '',
                'LatestTimeReady'   => '',
                'ContactInfo' => array(
                  'Name' => '',
                  'PhoneNumber' => '',
                  ),
                ),
              ),
              */
            ),
          'Package' => array(
            'PackagingType' => array(
              'Code' => '02',
              ),
            'Dimensions' => array(
              'UnitOfMeasurement' => array(
                'Code' => 'IN',
                ),
              'Length' => '',
              'Width'  => '',
              'Height' => '',
              ),
            'PackageWeight' => array(
              'Weight' => '',
              ),
            'ReferenceNumber' => array(
              'Code'  => '02',
              'Value' => '1234567',
              ),
            'PackageServiceOptions' => array(
              'InsuredValue' => array(
                'CurrencyCode'  => 'USD',
                'MonetaryValue' => ''
                ),
              'VerbalConfirmation' => array(
                'Name' => '',
                'PhoneNumber' => '',
                ),
              ),
            ),
          ),
        'LabelSpecification' => array(
          'LabelPrintMethod' => array(
            'Code' => 'GIF',
            ),
          'HTTPUserAgent' => 'Mozilla/4.5',
          'LabelImageFormat' => array(
            'Code' => 'GIF',
            ),
          ),
        ),
      ), $data);

    /**
     * Package XML
     */
    $xmlBuilder = new \UPS\XMLBuilder();
    $xml = $this->connector->getAccessRequestXMLString()
         . $xmlBuilder->buildXmlFromArray($this->dataSent);
		$this->xmlSent = $xml;

    /**
     * Process Request
     */
		$this->xmlResponse = $this->connector->sendEndpointXML('ShipConfirm', $xml);
    $this->dataResponse = new \SimpleXMLElement($this->xmlResponse);

    /**
     * Catch Error
     */
    if( $this->dataResponse && @$this->dataResponse->Response->Error ){
      $this->error = true;
      $this->message = $this->dataResponse->Response->Error->ErrorCode .': '. $this->dataResponse->Response->Error->ErrorDescription;
    }

    return $this->dataResponse;
	}

  function getShipmentAcceptance( $data = array() ) {

    /**
     * Merge Data
     */
    $this->dataSent = Common::mergeNested(array(
      'ShipmentAcceptRequest' => array(
        'Request' => array(
          'TransactionReference' => array(
            'XpciVersion' => 'guidlikesubstance',
            'XpciVersion' => '1.0001',
            ),
          'RequestAction' => 'ShipAccept',
          ),
        'ShipmentDigest' => '',
        ),
      ), $data);

    /**
     * Package XML
     */
    $xmlBuilder = new \UPS\XMLBuilder();
    $xml = $this->connector->getAccessRequestXMLString()
         . $xmlBuilder->buildXmlFromArray($this->dataSent);
    $this->xmlSent = $xml;

    /**
     * Process Request
     */
    $this->xmlResponse = $this->connector->sendEndpointXML('ShipAccept', $xml);
    $this->dataResponse = new \SimpleXMLElement($this->xmlResponse);

    /**
     * Catch Error
     */
    if( $this->dataResponse && @$this->dataResponse->Response->Error ){
      $this->error = true;
      $this->message = $this->dataResponse->Response->Error->ErrorCode .': '. $this->dataResponse->Response->Error->ErrorDescription;
    }

    return $this->dataResponse;
  }

	/**
	 * Old Methods
   *
   *
   *
   *
   *

	function buildRequestXML() {
		$xml = $this->connector->getAccessRequestXMLString();
		$ShipmentConfirmRequestXML = new \UPS\XMLBuilder();
		$ShipmentConfirmRequestXML->push('ShipmentConfirmRequest');
			$ShipmentConfirmRequestXML->push('Request');
				$ShipmentConfirmRequestXML->push('TransactionReference');
					$ShipmentConfirmRequestXML->element('CustomerContext', 'guidlikesubstance');
					$ShipmentConfirmRequestXML->element('XpciVersion', '1.0001');
				$ShipmentConfirmRequestXML->pop();
				$ShipmentConfirmRequestXML->element('RequestAction', 'ShipConfirm');
				$ShipmentConfirmRequestXML->element('RequestOption', 'nonvalidate');
				$ShipmentConfirmRequestXML->pop(); // end Request
			$ShipmentConfirmRequestXML->push('Shipment');
				$ShipmentConfirmRequestXML->push('Shipper');
					$ShipmentConfirmRequestXML->element('Name', 'Joes Garage');
					$ShipmentConfirmRequestXML->element('AttentionName', 'Mark Sanborn');
					$ShipmentConfirmRequestXML->element('ShipperNumber', '300YA9');
					$ShipmentConfirmRequestXML->push('Address');
						$ShipmentConfirmRequestXML->element('AddressLine1', '10000 Preston Rd');
						$ShipmentConfirmRequestXML->element('City', 'Whitehall');
						$ShipmentConfirmRequestXML->element('StateProvinceCode', 'MT');
						$ShipmentConfirmRequestXML->element('PostalCode', '59759');
					$ShipmentConfirmRequestXML->pop(); // end Address
				$ShipmentConfirmRequestXML->pop(); // end Shipper
				$ShipmentConfirmRequestXML->push('ShipTo');
					$ShipmentConfirmRequestXML->element('CompanyName', 'Pep Boys');
					$ShipmentConfirmRequestXML->element('AttentionName', 'Manny');
					$ShipmentConfirmRequestXML->push('PhoneNumber');
						$ShipmentConfirmRequestXML->push('StructuredPhoneNumber');
							$ShipmentConfirmRequestXML->element('PhoneDialPlanNumber', '410');
							$ShipmentConfirmRequestXML->element('PhoneLineNumber', '5551212');
							$ShipmentConfirmRequestXML->element('PhoneExtension', '1234');
						$ShipmentConfirmRequestXML->pop(); // end StrurcturedPhoneNumber
					$ShipmentConfirmRequestXML->pop(); // end PhoneNumber
					$ShipmentConfirmRequestXML->push('Address');
						$ShipmentConfirmRequestXML->element('AddressLine1', '201 York Rd');
						$ShipmentConfirmRequestXML->element('City', 'Timonium');
						$ShipmentConfirmRequestXML->element('StateProvinceCode', 'MD');
						$ShipmentConfirmRequestXML->element('CountryCode', 'US');
						$ShipmentConfirmRequestXML->element('PostalCode', '21093');
						$ShipmentConfirmRequestXML->element('ResidentialAddress', '');
					$ShipmentConfirmRequestXML->pop(); // end Address
				$ShipmentConfirmRequestXML->pop(); // end ShipTo
				$ShipmentConfirmRequestXML->push('Service');
					$ShipmentConfirmRequestXML->element('Code', '03');
					$ShipmentConfirmRequestXML->element('Description', 'UPS Ground');
				$ShipmentConfirmRequestXML->pop(); // end Service
				$ShipmentConfirmRequestXML->push('PaymentInformation');
					$ShipmentConfirmRequestXML->push('Prepaid');
						$ShipmentConfirmRequestXML->push('BillShipper');
							$ShipmentConfirmRequestXML->push('CreditCard');
								$ShipmentConfirmRequestXML->element('Type', '06');
								$ShipmentConfirmRequestXML->element('Number', '4111111111111111');
								$ShipmentConfirmRequestXML->element('ExpirationDate', '011909');
							$ShipmentConfirmRequestXML->pop(); // end CreditCard
						$ShipmentConfirmRequestXML->pop(); // end BillShipper
					$ShipmentConfirmRequestXML->pop(); // end Prepaid
				$ShipmentConfirmRequestXML->pop(); // end PaymentInformation
				$ShipmentConfirmRequestXML->push('ShipmentServiceOptions');
					$ShipmentConfirmRequestXML->push('OnCallAir');
						$ShipmentConfirmRequestXML->push('PickupDetails');
							$ShipmentConfirmRequestXML->element('PickupDate', '20090115');
							$ShipmentConfirmRequestXML->element('EarliestTimeReady', '0945');
							$ShipmentConfirmRequestXML->element('LatestTimeReady', '1445');
							$ShipmentConfirmRequestXML->push('ContactInfo');
								$ShipmentConfirmRequestXML->element('Name', 'JaneSmith');
								$ShipmentConfirmRequestXML->element('PhoneNumber', '9725551234');
							$ShipmentConfirmRequestXML->pop(); // end ContactInfo
						$ShipmentConfirmRequestXML->pop(); // end PickupDetails
					$ShipmentConfirmRequestXML->pop(); // end OnCallAir
				$ShipmentConfirmRequestXML->pop(); // end ShipmentServiceOptions
				$ShipmentConfirmRequestXML->push('Package');
					$ShipmentConfirmRequestXML->push('PackagingType');
						$ShipmentConfirmRequestXML->element('Code', '02');
					$ShipmentConfirmRequestXML->pop(); // end PackagingType
					$ShipmentConfirmRequestXML->push('Dimensions');
						$ShipmentConfirmRequestXML->push('UnitOfMeasurement');
							$ShipmentConfirmRequestXML->element('Code', 'IN');
						$ShipmentConfirmRequestXML->pop(); // end UnitOfMeasurement
						$ShipmentConfirmRequestXML->element('Length', '22');
						$ShipmentConfirmRequestXML->element('Width', '20');
						$ShipmentConfirmRequestXML->element('Height', '18');
					$ShipmentConfirmRequestXML->pop(); // end Dimensions
					$ShipmentConfirmRequestXML->push('PackageWeight');
						$ShipmentConfirmRequestXML->element('Weight', '14.1');
					$ShipmentConfirmRequestXML->pop(); // end PackageWeight
					$ShipmentConfirmRequestXML->push('ReferenceNumber');
						$ShipmentConfirmRequestXML->element('Code', '02');
						$ShipmentConfirmRequestXML->element('Value', '1234567');
					$ShipmentConfirmRequestXML->pop(); // end ReferenceNumber
					$ShipmentConfirmRequestXML->push('PackageServiceOptions');
						$ShipmentConfirmRequestXML->push('InsuredValue');
							$ShipmentConfirmRequestXML->element('CurrencyCode', 'USD');
							$ShipmentConfirmRequestXML->element('MonetaryValue', '149.99');
						$ShipmentConfirmRequestXML->pop(); // End Insured Value
						// $ShipmentConfirmRequestXML->push('VerbalConfirmation');
						// 	$ShipmentConfirmRequestXML->element('Name', 'SidneySmith');
						// 	$ShipmentConfirmRequestXML->element('PhoneNumber', '4105551234');
						// $ShipmentConfirmRequestXML->pop(); // end VerbalConfirmation
					$ShipmentConfirmRequestXML->pop(); // end PackageServiceOptions
				$ShipmentConfirmRequestXML->pop(); // end Package
			$ShipmentConfirmRequestXML->pop(); // end Shipment
			$ShipmentConfirmRequestXML->push('LabelSpecification');
				$ShipmentConfirmRequestXML->push('LabelPrintMethod');
					$ShipmentConfirmRequestXML->element('Code', 'GIF');
				$ShipmentConfirmRequestXML->pop(); // end LabelPrintMethod
				$ShipmentConfirmRequestXML->element('HTTPUserAgent', 'Mozilla/4.5');
				$ShipmentConfirmRequestXML->push('LabelImageFormat');
					$ShipmentConfirmRequestXML->element('Code', 'GIF');
				$ShipmentConfirmRequestXML->pop(); // end LabelImageFormat
			$ShipmentConfirmRequestXML->pop(); // end LabelSpecification
		$ShipmentConfirmRequestXML->pop(); // ShipmentConfirmRequest
		$xml .= $ShipmentConfirmRequestXML->getXml();
		$xmlResponse = $this->connector->sendEndpointXML('ShipConfirm', $xml);
		$this->xmlSent = $xml;
		$this->xmlResponse = $xmlResponse;
		return $xmlResponse;
	}

	function buildShipmentAcceptXML($ShipmentDigest) {
		$xml = new \UPS\XMLBuilder();
		$xml->push('ShipmentAcceptRequest');
			$xml->push('Request');
				$xml->push('TransactionReference');
					$xml->element('CustomerContext', 'guidlikesubstance');
					$xml->element('XpciVersion', '1.0001');
				$xml->pop(); // end TransactionReference
			$xml->element('RequestAction', 'ShipAccept');
			$xml->pop(); // end Request
		$xml->element('ShipmentDigest', $ShipmentDigest);
		$xml->pop(); // end ShipmentAcceptRequest
		$ShipmentAcceptXML = $this->connector->getAccessRequestXMLString();
		$ShipmentAcceptXML .= $xml->getXml();
		$xmlResponse = $this->connector->sendEndpointXML('ShipAccept', $ShipmentAcceptXML);
		$this->xmlResponse = $xmlResponse;
		return $ShipmentAcceptXML;
	}

	function responseArray() {
		$xmlParser = new \UPS\XMLParser();
		$responseArray = $xmlParser->xmlParser($this->xmlResponse);
		$responseArray = $xmlParser->getData();
		return $responseArray;
	}

  */

}


text:
MONGO_URL = 'localhost'
MONGO_DB = 'meinv'
MONGO_TABLE = 'meizitu'
MONGO_QUEUE_TABLE = 'meizitu_queue'

redpajama_set_name: RedPajamaGithub


text:
package boofcv.abst.filter.derivative;

import boofcv.alg.InputSanityCheck;
import boofcv.alg.filter.convolve.ConvolveImageNoBorder;
import boofcv.alg.filter.convolve.border.ConvolveJustBorder_General;
import boofcv.alg.filter.derivative.DerivativeHelperFunctions;
import boofcv.alg.filter.derivative.impl.GradientSobel_Outer;
import boofcv.alg.filter.derivative.impl.GradientSobel_UnrolledOuter;
import boofcv.struct.image.ImageBase;

/**
 * A generic interface for computing first order image derivative along the x and y axes.
 *
 * @author Peter Abeles
 */
public interface ImageGradient<Input extends ImageBase, Output extends ImageBase> extends ImageDerivative<Input,Output> {

	/**
	 * Computes the image gradient from the input image and stores the results into
	 * 'derivX' and 'derivY'
	 *
	 * @param inputImage Original input image. Not modified.
	 * @param derivX First order image derivative along the x-axis. Modified.
	 * @param derivY First order image derivative along the y-axis. Modified.
	 */
	public void process(Input inputImage , Output derivX, Output derivY, InputSanityCheck ISC, DerivativeHelperFunctions DHF, ConvolveImageNoBorder CINB, ConvolveJustBorder_General CJBG,
						GradientSobel_Outer GSO, GradientSobel_UnrolledOuter GSUO);

}


text:
#if !defined __adjmatrix_H__
#define __adjmatrix_H__

#include "mysimple.h"
#include "prjsupport.h"
#include "bitvector.h"
#include "web.h"
#include "regspec.h"

/**
 * Encapsulates the adjmatrix  of the node
 */
class AdjMatrix  {
public:

  /**
   * Constructs a AdjMatrix
   */
  AdjMatrix( WebList &webList,
             BitVectorMap &inMap,
             BitVectorMap &outMap );

  /**
   * Destroys the adjmatrix
   */
  ~AdjMatrix ();

  /**
   * builds the adjacency matrix
   */
  void build();

  /**
   * Returns true if two symbolic registers interfere with
   * each other
   */
  bool isSymRegInterfere( int reg1, int reg2 );

  /**
   * returns a new adjmatrix for the coalesced register.
   * It is the caller's reposibility to delete the
   * newly created object
   */
  AdjMatrix *getCoalescedMatrix( my_simple_reg *coalescedReg,
                                 my_simple_reg *coalescedWithReg );

  /**
   * Sets the row of the adjacency matrix. The size of the
   * vector must be same as the matrix dimension
   */
  bool setRow( int row, BoolVector& vctRow, bool coalesced = false );

  /**
   * returns true/false value for given row and column
   */
  bool getValue( int row, int col ) const;

  /**
   * returns the weblist
   */
  WebList getWebList() const;

  /**
   * Builds the spill interference. This method looks at the
   * weblist and constraints to redo the machine register
   * interference.
   */
  void buildSpillInterference();

  /**
   * prints the lower triangular matrix
   */
  void print( FILE *fd, bool lower = false ) const;

private:
  /**
   * builds the adjacency matrix
   */
  void build( const ConstraintVector &vct );

  /**
   * Builds the interference for the live variable map
   */
  void buildInterference( BitVectorMap &bitMap );

  /**
   * Builds the interference for the bit vector
   */
  void buildInterference( BitVector &bitVector );

  /**
   * Builds the machine register to symbolic register
   * interference
   */
  void buildRegisterInterference( const ConstraintVector &vct );

  /**
   * Makes the constraint for the Machine Register that
   * has no constraints
   */
  void interferenceWithNoConstraints( int machineReg,
                                      const ConstraintVector &vct );

  /**
   * Makes the constraint for the Machine Register that
   * has constraints
   */
  void interferenceWithConstraints( int machineReg,
                                    const ConstraintVector &vct );

  /**
   * returns true if the machine register has constraint
   * in the reg file
   */
  bool hasConstraints(int machineReg,
                      const ConstraintVector &vct );

  /**
   * Unions the boolean vector
   */
  BoolVector unionVector( BoolVector &b1,
                          BoolVector &b2,
                          int startFrom = 0 );

  /**
   * Removes an item from the BoolVector identified by
   * the index
   */
  void removeItemFromVector( BoolVector &vct, int pos );

  int _dim;
  BoolMatrix _adjMatrix;
  WebList _webList;
  BitVectorMap _inMap;
  BitVectorMap _outMap;
  IntMap _mRegIndexMap; //symbolic register to index map
  IntMap _sRegIndexMap; //symbolic register to index map
};

#endif //__adjmatrix_H__


text:
<!DOCTYPE html>

  <title>Swagger UI</title>
  <link href='//fonts.googleapis.com/css?family=Droid+Sans:400,700' rel='stylesheet' type='text/css'/>
  <link href='css/highlight.default.css' media='screen' rel='stylesheet' type='text/css'/>
  <link href='css/screen.css' media='screen' rel='stylesheet' type='text/css'/>

    Assemblage

    <form id='api_selector'>

<input placeholder="http://example.com/api" id="input_baseUrl" name="baseUrl" type="text"/>

<input placeholder="api_key" id="input_apiKey" name="apiKey" type="text"/>

Explore

    </form>


text:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const user_1 = require("./user");
const post_1 = require("./post");
const tag_1 = require("./tag");
let router = express_1.Router();
// router.use(home);
router.use(user_1.user);
router.use(post_1.post);
router.use(tag_1.tag);
// router.use(admin);
// router.get('/:title', postDetail);
// router.get('/tag/:tagName', tag)
// router.use('/login', login);
exports.default = router;
//# sourceMappingURL=index.js.map


text:
"""
BSON serialization and deserialization logic.
Specifications taken from: http://bsonspec.org/#/specification
The following types are unsupported, because for data exchange purposes, they're
over-engineered:
	0x06 (Undefined)
	0x07 (ObjectId)
	0x0b (Regex - Exactly which flavor do you want? Better let higher level
		programmers make that decision.)
	0x0c (DBPointer)
	0x0d (JavaScript code)
	0x0e (Symbol)
	0x0f (JS w/ scope)
	0x11 (MongoDB-specific timestamp)

For binaries, only the default 0x0 type is supported.
"""

from codec import *
__all__ = ["loads", "dumps"]

# {{{ Public API
def dumps(obj):
	"""
	Given a dict, outputs a BSON string.
	"""
	return encode_document(obj)

def loads(data):
	"""
	Given a BSON string, outputs a dict.
	"""
	return decode_document(data, 0)[1]
# }}}


text: after window.onload, innerHTML


text:
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("vProto")]
[assembly: AssemblyDescription("Network protocol for the .NET Framework.")]
[assembly: AssemblyCompany("Vercas")]
[assembly: AssemblyProduct("vProto")]
[assembly: AssemblyCopyright("Copyright © 2013 Alexandru-Mihai Maftei")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

#if DEBUG
#if NET_4_5
[assembly: AssemblyConfiguration("Debug @ .NET 4.5")]
#elif NET_4_0
[assembly: AssemblyConfiguration("Debug @ .NET 4.0")]
#endif
#else
#if NET_4_5
[assembly: AssemblyConfiguration("Release @ .NET 4.5")]
#elif NET_4_0
[assembly: AssemblyConfiguration("Release @ .NET 4.0")]
#endif
#endif

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components.  If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("b7275194-3b44-4b9e-b6af-088ad51de304")]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]


text:
require File.expand_path('../application', __FILE__)

# Initialize the Rails application.
DecksterTest::Application.initialize!


text:
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Widgets module"
// For conditions of distribution and use, see copyright notice in Config.hpp

#include <Nazara/Widgets/Widgets.hpp>
#include <Nazara/Graphics/BasicMaterial.hpp>
#include <Nazara/Graphics/Material.hpp>
#include <Nazara/Graphics/MaterialPass.hpp>
#include <Nazara/Widgets/Debug.hpp>

namespace Nz
{
	/*!
	* \ingroup widgets
	* \class Nz::Widgets
	* \brief Widgets class that represents the module initializer of Widgets
	*/
	Widgets::Widgets(Config /*config*/) :
	ModuleBase("Widgets", this)
	{
		CreateDefaultMaterials();
	}

	void Widgets::CreateDefaultMaterials()
	{
		m_opaqueMaterialPass = std::make_shared<MaterialPass>(BasicMaterial::GetSettings());
		m_opaqueMaterialPass->EnableDepthBuffer(true);
		m_opaqueMaterialPass->EnableDepthWrite(false);
		m_opaqueMaterialPass->EnableScissorTest(true);

		m_opaqueMaterial = std::make_shared<Material>();
		m_opaqueMaterial->AddPass("ForwardPass", m_opaqueMaterialPass);

		m_transparentMaterialPass = std::make_shared<MaterialPass>(BasicMaterial::GetSettings());
		m_transparentMaterialPass->EnableDepthBuffer(true);
		m_transparentMaterialPass->EnableDepthWrite(false);
		m_transparentMaterialPass->EnableScissorTest(true);
		m_transparentMaterialPass->EnableBlending(true);
		m_transparentMaterialPass->SetBlendEquation(BlendEquation::Add, BlendEquation::Add);
		m_transparentMaterialPass->SetBlendFunc(BlendFunc::SrcAlpha, BlendFunc::InvSrcAlpha, BlendFunc::One, BlendFunc::One);

		m_transparentMaterial = std::make_shared<Material>();
		m_transparentMaterial->AddPass("ForwardPass", m_transparentMaterialPass);
	}

	Widgets* Widgets::s_instance = nullptr;
}


text:
/** \file     TEncPic.cpp
    \brief    class of picture which includes side information for encoder
*/

#include "TEncPic.h"

//! \ingroup TLibEncoder
//! \{

/** Constructor
 */
TEncQPAdaptationUnit::TEncQPAdaptationUnit()
: m_dActivity(0.0)
{
}

/** Destructor
 */
TEncQPAdaptationUnit::~TEncQPAdaptationUnit()
{
}

/** Constructor
 */
TEncPicQPAdaptationLayer::TEncPicQPAdaptationLayer()
: m_uiAQPartWidth(0)
, m_uiAQPartHeight(0)
, m_uiNumAQPartInWidth(0)
, m_uiNumAQPartInHeight(0)
, m_acTEncAQU(NULL)
, m_dAvgActivity(0.0)
{
}

/** Destructor
 */
TEncPicQPAdaptationLayer::~TEncPicQPAdaptationLayer()
{
  destroy();
}

/** Initialize member variables
 * \param iWidth Picture width
 * \param iHeight Picture height
 * \param uiAQPartWidth Width of unit block for analyzing local image characteristics
 * \param uiAQPartHeight Height of unit block for analyzing local image characteristics
 * \return Void
 */
Void TEncPicQPAdaptationLayer::create( Int iWidth, Int iHeight, UInt uiAQPartWidth, UInt uiAQPartHeight )
{
  m_uiAQPartWidth = uiAQPartWidth;
  m_uiAQPartHeight = uiAQPartHeight;
  m_uiNumAQPartInWidth = (iWidth + m_uiAQPartWidth-1) / m_uiAQPartWidth;
  m_uiNumAQPartInHeight = (iHeight + m_uiAQPartHeight-1) / m_uiAQPartHeight;
  m_acTEncAQU = new TEncQPAdaptationUnit[ m_uiNumAQPartInWidth * m_uiNumAQPartInHeight ];
}

/** Clean up
 * \return Void
 */
Void TEncPicQPAdaptationLayer::destroy()
{
  if (m_acTEncAQU)
  {
    delete[] m_acTEncAQU;
    m_acTEncAQU = NULL;
  }
}

/** Constructor
 */
TEncPic::TEncPic()
: m_acAQLayer(NULL)
, m_uiMaxAQDepth(0)
{
}

/** Destructor
 */
TEncPic::~TEncPic()
{
  destroy();
}

/** Initialize member variables
 * \param iWidth Picture width
 * \param iHeight Picture height
 * \param uiMaxWidth Maximum CU width
 * \param uiMaxHeight Maximum CU height
 * \param uiMaxDepth Maximum CU depth
 * \param uiMaxAQDepth Maximum depth of unit block for assigning QP adaptive to local image characteristics
 * \param bIsVirtual
 * \return Void
 */
Void TEncPic::create( Int iWidth, Int iHeight, ChromaFormat chromaFormat, UInt uiMaxWidth, UInt uiMaxHeight, UInt uiMaxDepth, UInt uiMaxAQDepth,
                      Window &conformanceWindow, Window &defaultDisplayWindow, Int *numReorderPics, Bool bIsVirtual )
{
  TComPic::create( iWidth, iHeight, chromaFormat, uiMaxWidth, uiMaxHeight, uiMaxDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, bIsVirtual );
  m_uiMaxAQDepth = uiMaxAQDepth;
  if ( uiMaxAQDepth > 0 )
  {
    m_acAQLayer = new TEncPicQPAdaptationLayer[ m_uiMaxAQDepth ];
    for (UInt d = 0; d < m_uiMaxAQDepth; d++)
    {
      m_acAQLayer[d].create( iWidth, iHeight, uiMaxWidth>>d, uiMaxHeight>>d );
    }
  }
}

/** Clean up
 * \return Void
 */
Void TEncPic::destroy()
{
  if (m_acAQLayer)
  {
    delete[] m_acAQLayer;
    m_acAQLayer = NULL;
  }
  TComPic::destroy();
}
//! \}


text:
package com.github.musikk.classreader.attributes;

import com.github.musikk.classreader.ClassReader;
import com.github.musikk.classreader.ClassReaderContext;
import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;

public class RuntimeVisibleParameterAnnotations extends AttributeInfo {

	private final Multimap<Integer, Annotation> parameterAnnotations;

	public RuntimeVisibleParameterAnnotations(Multimap<Integer, Annotation> parameterAnnotations) {
		this.parameterAnnotations = parameterAnnotations;
	}

	public Multimap<Integer, Annotation> getParameterAnnotations() {
		return Multimaps.unmodifiableMultimap(parameterAnnotations);
	}

	static RuntimeVisibleParameterAnnotations getRuntimeVisibleParameterAnnotations(ClassReaderContext ctxt) {
		ClassReader reader = ctxt.getClassReader();

		int numParameters = reader.readUnsignedByte();
		Multimap<Integer, Annotation> parameterAnnotations = LinkedListMultimap.create();
		for (int i = 0; i < numParameters; i++) {
			int numAnnotations = reader.readUnsignedShort();
			for (int j = 0; j < numAnnotations; j++) {
				parameterAnnotations.put(i, Annotation.getAnnotation(ctxt));
			}
		}

		return new RuntimeVisibleParameterAnnotations(parameterAnnotations);
	}

}


text:
import { ServerCollection } from "./../data/model/server-collection";

export function addItem<T>(state: ServerCollection<T>, toAdd: T, idName: string): ServerCollection<T> {
		let updateIndex = state.data.findIndex((x) => {
				return x[idName] == toAdd[idName];
		});

		if (updateIndex != -1) {
				let toUpdate = state.data[updateIndex];
				toUpdate = Object.assign(toUpdate, toAdd);
				return tassign(state);
		}

		return state;
}

export function deleteItem<T>(state: ServerCollection<T>, toDelete: T, idName: string): ServerCollection<T> {
		let deleteIndex = state.data.findIndex((user) => {
				return user[idName] == toDelete[idName];
		});

		if (deleteIndex !== -1) {
				state.data.splice(deleteIndex, 1);
				return tassign(state);
		}

		return state;
}

export function tassign<T extends U, U>(target: T, ...source: U[]): T {
	return Object.assign({}, target, ...source);
}


text:
class State
{
public:
	virtual bool init() =0;
	virtual void run() =0;
	bool initialized;

protected:
	Game* game;
	View* view;
	Engine* engine;
};


text:
/**
 * Created by maksim on 1/21/17.
 */
import { initState } from './stateInit';
import { createStore } from 'redux';
import { MoviesApp } from './reducers/MoviesApp';

export const configureApp = () => {
    return new Promise((resolve,reject) => {
        initState()
            .then( initialState => {
                resolve(createStore(MoviesApp,initialState))
            });
    })
};


text:
// Copyright (c) Benjamin Proemmer. All rights reserved.
// See License in the project root for license information.

namespace Dacs7.Domain
{

    public enum DataTransportSize
    {
        Null = 0,
        Bit = 3, //bit access, length is in bits
        Byte = 4, //byte/word/dword access, length is in bits
        Int = 5, //integer access, length is in bits
        Dint = 6, //integer access, length is in bytes
        Real = 7, //real access, length is in bytes
        OctetString = 9 //octet string, length is in bytes
    }

}


text:
> Changelog using default 'CHANGELOG' file. See 'examples/changelog.js'

> Changelog using custom data source

{%= changelog('changelog.yaml') %}


text:
package ua.com.fielden.platform.web.centre.api.crit.defaults.mnemonics.construction.single;

public interface ISingleCritDateValueMnemonic4MissingValue extends ISingleCritDateValueMnemonic5Value {
    ISingleCritDateValueMnemonic5Value canHaveNoValue();
}


text:
layout: bt_wiki
title: vCloud Manager Blueprint Reference
category: Manager Blueprints
publish: true
abstract: "Reference on how to bootstrap a Cloudify manager on vCloud"
pageord: 1000
---

{{% gsSummary %}}

{{% gsNote title="Note" %}}
This reference only explains the structure and various values in the blueprint. For better understanding of it, make yourself familiar with [Cloudify blueprints DSL](dsl-spec-general.html), the [Cloudify vCloud plugin](plugin-vcloud.html), and the [Manager Blueprints Authoring guide](getting-started-write-blueprint.html).
{{% /gsNote %}}

# Inputs

## Required inputs

* `vcloud_username` Username on vCloud.
* `vcloud_password` Password on vCloud.
* `vcloud_url` vCloud URL.
* `vcloud_service` vCloud service name
* `vcloud_org` vCloud organization name
* `vcloud_vdc` vCloud Virtual Datacenter name
* `manager_server_name` Manager name
* `manager_server_catalog` VApp templates catalog
* `manager_server_template` VApp template from which server will be spawned
* `management_network_name` management network name
* `edge_gateway` Edge Gateway name
* `manager_public_key` manager public ssh key
* `agent_public_key` agent public ssh key

## Optional inputs
* `vcloud_service_type` vCloud service type. Default is 'subscription'.
* `vcloud_region` OnDemand region name.
* `vcloud_org_url` Only required if using token based login on a private vcloud director. This can be obtained by following the vcloud API example docs.
* `manager_server_cpus` manager VM cpu count. Default is 2.
* `manager_server_memory` manager VM memory size. Default is 4096.
* `management_network_use_existing` Default is false.
* `management_port_ip_allocation_mode` IP allocation mode for manager Port (Default: `dhcp`).
* `management_port_ip_address` If IP allocation mode was set to `manual` this will be used as Port ip address (Default: `''`).
* `management_network_public_ip` Management network public ip to allow internet access from the network. If empty string is specified, then public will be allocated from a pool of free public ips (Default: `''`).
* `management_network_public_nat_use_existing` Default is false.
* `floating_ip_public_ip` Manager public ip. If empty string is specified, then public will be allocated from a pool of free public ips (Default: `''`).
* `manager_server_user` The name of the user that will be used to access the Cloudify manager (Default: `ubuntu`).
* `manager_private_key_path` The path on the local machine to the private key file that will be used with the Cloudify manager (Default: `~/.ssh/cloudify-manager-kp.pem`).
* `agent_private_key_path` The path on the local machine to the private key file that will be used with Cloudify agents (Default: `~/.ssh/cloudify-agent-kp.pem`).
* `agents_user` The default username to be used when connecting into applications' agent VMs (for agent installtion).
* `resources_prefix` Resource prefix to be attached to cloud resources' names.

# Topology

The blueprint builds the following topology on vCloud:

  - vCloud routed network for communication between the Cloudify manager and Cloudify agent machines.
  - A floating IP for the Cloudify manager machine.
  - A port to connect Cloudify Manager to management network.
  - A server which will host the Cloudify manager.

# Nodes

The blueprint contains the following nodes:

  - *management_network* - A vCloud routed network for communication between the Cloudify manager and agent machines.
  - *management_network_internet_access* - SNAT rule to enable internet access from management network.
  - *management_port* - connection between Manager Server and Management Network.
  - *manager_floating_ip* - A floating IP which will be associated with the Cloudify manager machine.
  - *manager_server* - The server on which the Cloudify manager will be installed.
  - *vcloud_configuration* - A node which represents configuration settings for connecting with vCloud.
  - *manager* - The node which represents the manager. You may find more information about this node in the [Types Reference](#reference-types.html#cloudifymanager-type) section as well as in the [Manager Blueprints Authoring guide](getting-started-write-blueprint.html).

# Configuration Operations

The *manager's* node *configure* lifecycle operation is mapped to a method in the configure.py module. It creates a file on the Cloudify manager server, which holds the configuration settings for connecting with vCloud. This file can be used by the vCloud plugin when installing applications.


text:
<?php

namespace CultuurNet\UDB3\Role\Events;

use PHPUnit\Framework\TestCase;
use ValueObjects\Identity\UUID;
use ValueObjects\StringLiteral\StringLiteral;

class RoleCreatedTest extends TestCase
{
    /**
     * @var UUID
     */
    protected $uuid;

    /**
     * @var StringLiteral
     */
    protected $name;

    /**
     * @var RoleCreated
     */
    protected $roleCreated;

    protected function setUp()
    {
        $this->uuid = new UUID();

        $this->name = new StringLiteral('roleName');

        $this->roleCreated = new RoleCreated(
            $this->uuid,
            $this->name
        );
    }

    /**
     * @test
     */
    public function it_extends_an_event()
    {
        $this->assertTrue(is_subclass_of(
            $this->roleCreated,
            AbstractEvent::class
        ));
    }

    /**
     * @test
     */
    public function it_stores_a_uuid()
    {
        $this->assertEquals($this->uuid, $this->roleCreated->getUuid());
    }

    /**
     * @test
     */
    public function it_stores_a_name()
    {
        $this->assertEquals($this->name, $this->roleCreated->getName());
    }

    /**
     * @test
     */
    public function it_can_deserialize()
    {
        $created = RoleCreated::deserialize($this->createdAsArray());

        $this->assertEquals($this->roleCreated, $created);
    }

    /**
     * @test
     */
    public function it_can_serialize()
    {
        $createdAsArray = $this->roleCreated->serialize();

        $this->assertEquals($this->createdAsArray(), $createdAsArray);
    }

    /**
     * @return array
     */
    protected function createdAsArray()
    {
        return [
            'uuid' => $this->roleCreated->getUuid()->toNative(),
            'name' => $this->roleCreated->getName()->toNative(),
        ];
    }
}


text:
package fasthttp

import (
	"bufio"
	"crypto/tls"
	"errors"
	"fmt"
	"io"
	"log"
	"mime/multipart"
	"net"
	"os"
	"strings"
	"sync"
	"sync/atomic"
	"time"
)

// ServeConn serves HTTP requests from the given connection
// using the given handler.
//
// ServeConn returns nil if all requests from the c are successfully served.
// It returns non-nil error otherwise.
//
// Connection c must immediately propagate all the data passed to Write()
// to the client. Otherwise requests' processing may hang.
//
// ServeConn closes c before returning.
func ServeConn(c net.Conn, handler RequestHandler) error {
	v := serverPool.Get()
	if v == nil {
		v = &Server{}
	}
	s := v.(*Server)
	s.Handler = handler
	err := s.ServeConn(c)
	s.Handler = nil
	serverPool.Put(v)
	return err
}

var serverPool sync.Pool

// Serve serves incoming connections from the given listener
// using the given handler.
//
// Serve blocks until the given listener returns permanent error.
func Serve(ln net.Listener, handler RequestHandler) error {
	s := &Server{
		Handler: handler,
	}
	return s.Serve(ln)
}

// ServeTLS serves HTTPS requests from the given net.Listener
// using the given handler.
//
// certFile and keyFile are paths to TLS certificate and key files.
func ServeTLS(ln net.Listener, certFile, keyFile string, handler RequestHandler) error {
	s := &Server{
		Handler: handler,
	}
	return s.ServeTLS(ln, certFile, keyFile)
}

// ServeTLSEmbed serves HTTPS requests from the given net.Listener
// using the given handler.
//
// certData and keyData must contain valid TLS certificate and key data.
func ServeTLSEmbed(ln net.Listener, certData, keyData []byte, handler RequestHandler) error {
	s := &Server{
		Handler: handler,
	}
	return s.ServeTLSEmbed(ln, certData, keyData)
}

// ListenAndServe serves HTTP requests from the given TCP addr
// using the given handler.
func ListenAndServe(addr string, handler RequestHandler) error {
	s := &Server{
		Handler: handler,
	}
	return s.ListenAndServe(addr)
}

// ListenAndServeUNIX serves HTTP requests from the given UNIX addr
// using the given handler.
//
// The function deletes existing file at addr before starting serving.
//
// The server sets the given file mode for the UNIX addr.
func ListenAndServeUNIX(addr string, mode os.FileMode, handler RequestHandler) error {
	s := &Server{
		Handler: handler,
	}
	return s.ListenAndServeUNIX(addr, mode)
}

// ListenAndServeTLS serves HTTPS requests from the given TCP addr
// using the given handler.
//
// certFile and keyFile are paths to TLS certificate and key files.
func ListenAndServeTLS(addr, certFile, keyFile string, handler RequestHandler) error {
	s := &Server{
		Handler: handler,
	}
	return s.ListenAndServeTLS(addr, certFile, keyFile)
}

// ListenAndServeTLSEmbed serves HTTPS requests from the given TCP addr
// using the given handler.
//
// certData and keyData must contain valid TLS certificate and key data.
func ListenAndServeTLSEmbed(addr string, certData, keyData []byte, handler RequestHandler) error {
	s := &Server{
		Handler: handler,
	}
	return s.ListenAndServeTLSEmbed(addr, certData, keyData)
}

// RequestHandler must process incoming requests.
//
// RequestHandler must call ctx.TimeoutError() before returning
// if it keeps references to ctx and/or its' members after the return.
// Consider wrapping RequestHandler into TimeoutHandler if response time
// must be limited.
type RequestHandler func(ctx *RequestCtx)

// Server implements HTTP server.
//
// Default Server settings should satisfy the majority of Server users.
// Adjust Server settings only if you really understand the consequences.
//
// It is forbidden copying Server instances. Create new Server instances
// instead.
//
// It is safe to call Server methods from concurrently running goroutines.
type Server struct {
	noCopy noCopy

	// Handler for processing incoming requests.
	Handler RequestHandler

	// Server name for sending in response headers.
	//
	// Default server name is used if left blank.
	Name string

	// The maximum number of concurrent connections the server may serve.
	//
	// DefaultConcurrency is used if not set.
	Concurrency int

	// Whether to disable keep-alive connections.
	//
	// The server will close all the incoming connections after sending
	// the first response to client if this option is set to true.
	//
	// By default keep-alive connections are enabled.
	DisableKeepalive bool

	// Per-connection buffer size for requests' reading.
	// This also limits the maximum header size.
	//
	// Increase this buffer if your clients send multi-KB RequestURIs
	// and/or multi-KB headers (for example, BIG cookies).
	//
	// Default buffer size is used if not set.
	ReadBufferSize int

	// Per-connection buffer size for responses' writing.
	//
	// Default buffer size is used if not set.
	WriteBufferSize int

	// Maximum duration for reading the full request (including body).
	//
	// This also limits the maximum duration for idle keep-alive
	// connections.
	//
	// By default request read timeout is unlimited.
	ReadTimeout time.Duration

	// Maximum duration for writing the full response (including body).
	//
	// By default response write timeout is unlimited.
	WriteTimeout time.Duration

	// Maximum number of concurrent client connections allowed per IP.
	//
	// By default unlimited number of concurrent connections
	// may be established to the server from a single IP address.
	MaxConnsPerIP int

	// Maximum number of requests served per connection.
	//
	// The server closes connection after the last request.
	// 'Connection: close' header is added to the last response.
	//
	// By default unlimited number of requests may be served per connection.
	MaxRequestsPerConn int

	// Maximum keep-alive connection lifetime.
	//
	// The server closes keep-alive connection after its' lifetime
	// expiration.
	//
	// See also ReadTimeout for limiting the duration of idle keep-alive
	// connections.
	//
	// By default keep-alive connection lifetime is unlimited.
	MaxKeepaliveDuration time.Duration

	// Maximum request body size.
	//
	// The server rejects requests with bodies exceeding this limit.
	//
	// Request body size is limited by DefaultMaxRequestBodySize by default.
	MaxRequestBodySize int

	// Aggressively reduces memory usage at the cost of higher CPU usage
	// if set to true.
	//
	// Try enabling this option only if the server consumes too much memory
	// serving mostly idle keep-alive connections. This may reduce memory
	// usage by more than 50%.
	//
	// Aggressive memory usage reduction is disabled by default.
	ReduceMemoryUsage bool

	// Rejects all non-GET requests if set to true.
	//
	// This option is useful as anti-DoS protection for servers
	// accepting only GET requests. The request size is limited
	// by ReadBufferSize if GetOnly is set.
	//
	// Server accepts all the requests by default.
	GetOnly bool

	// Logs all errors, including the most frequent
	// 'connection reset by peer', 'broken pipe' and 'connection timeout'
	// errors. Such errors are common in production serving real-world
	// clients.
	//
	// By default the most frequent errors such as
	// 'connection reset by peer', 'broken pipe' and 'connection timeout'
	// are suppressed in order to limit output log traffic.
	LogAllErrors bool

	// Header names are passed as-is without normalization
	// if this option is set.
	//
	// Disabled header names' normalization may be useful only for proxying
	// incoming requests to other servers expecting case-sensitive
	// header names. See https://github.com/valyala/fasthttp/issues/57
	// for details.
	//
	// By default request and response header names are normalized, i.e.
	// The first letter and the first letters following dashes
	// are uppercased, while all the other letters are lowercased.
	// Examples:
	//
	//     * HOST -> Host
	//     * content-type -> Content-Type
	//     * cONTENT-lenGTH -> Content-Length
	DisableHeaderNamesNormalizing bool

	// Logger, which is used by RequestCtx.Logger().
	//
	// By default standard logger from log package is used.
	Logger Logger

	concurrency      uint32
	concurrencyCh    chan struct{}
	perIPConnCounter perIPConnCounter
	serverName       atomic.Value

	ctxPool        sync.Pool
	readerPool     sync.Pool
	writerPool     sync.Pool
	hijackConnPool sync.Pool
	bytePool       sync.Pool
}

// TimeoutHandler creates RequestHandler, which returns StatusRequestTimeout
// error with the given msg to the client if h didn't return during
// the given duration.
//
// The returned handler may return StatusTooManyRequests error with the given
// msg to the client if there are more than Server.Concurrency concurrent
// handlers h are running at the moment.
func TimeoutHandler(h RequestHandler, timeout time.Duration, msg string) RequestHandler {
	if timeout <= 0 {
		return h
	}

	return func(ctx *RequestCtx) {
		concurrencyCh := ctx.s.concurrencyCh
		select {
		case concurrencyCh <- struct{}{}:
		default:
			ctx.Error(msg, StatusTooManyRequests)
			return
		}

		ch := ctx.timeoutCh
		if ch == nil {
			ch = make(chan struct{}, 1)
			ctx.timeoutCh = ch
		}
		go func() {
			h(ctx)
			ch <- struct{}{}
			<-concurrencyCh
		}()
		ctx.timeoutTimer = initTimer(ctx.timeoutTimer, timeout)
		select {
		case <-ch:
		case <-ctx.timeoutTimer.C:
			ctx.TimeoutError(msg)
		}
		stopTimer(ctx.timeoutTimer)
	}
}

// CompressHandler returns RequestHandler that transparently compresses
// response body generated by h if the request contains 'gzip' or 'deflate'
// 'Accept-Encoding' header.
func CompressHandler(h RequestHandler) RequestHandler {
	return CompressHandlerLevel(h, CompressDefaultCompression)
}

// CompressHandlerLevel returns RequestHandler that transparently compresses
// response body generated by h if the request contains 'gzip' or 'deflate'
// 'Accept-Encoding' header.
//
// Level is the desired compression level:
//
//     * CompressNoCompression
//     * CompressBestSpeed
//     * CompressBestCompression
//     * CompressDefaultCompression
//     * CompressHuffmanOnly
func CompressHandlerLevel(h RequestHandler, level int) RequestHandler {
	return func(ctx *RequestCtx) {
		h(ctx)
		ce := ctx.Response.Header.PeekBytes(strContentEncoding)
		if len(ce) > 0 {
			// Do not compress responses with non-empty
			// Content-Encoding.
			return
		}
		if ctx.Request.Header.HasAcceptEncodingBytes(strGzip) {
			ctx.Response.gzipBody(level)
		} else if ctx.Request.Header.HasAcceptEncodingBytes(strDeflate) {
			ctx.Response.deflateBody(level)
		}
	}
}

// RequestCtx contains incoming request and manages outgoing response.
//
// It is forbidden copying RequestCtx instances.
//
// RequestHandler should avoid holding references to incoming RequestCtx and/or
// its' members after the return.
// If holding RequestCtx references after the return is unavoidable
// (for instance, ctx is passed to a separate goroutine and ctx lifetime cannot
// be controlled), then the RequestHandler MUST call ctx.TimeoutError()
// before return.
//
// It is unsafe modifying/reading RequestCtx instance from concurrently
// running goroutines. The only exception is TimeoutError*, which may be called
// while other goroutines accessing RequestCtx.
type RequestCtx struct {
	noCopy noCopy

	// Incoming request.
	//
	// Copying Request by value is forbidden. Use pointer to Request instead.
	Request Request

	// Outgoing response.
	//
	// Copying Response by value is forbidden. Use pointer to Response instead.
	Response Response

	userValues userData

	lastReadDuration time.Duration

	connID         uint64
	connRequestNum uint64
	connTime       time.Time

	time time.Time

	logger ctxLogger
	s      *Server
	c      net.Conn
	fbr    firstByteReader

	timeoutResponse *Response
	timeoutCh       chan struct{}
	timeoutTimer    *time.Timer

	hijackHandler HijackHandler
}

// HijackHandler must process the hijacked connection c.
//
// The connection c is automatically closed after returning from HijackHandler.
//
// The connection c must not be used after returning from the handler.
type HijackHandler func(c net.Conn)

// Hijack registers the given handler for connection hijacking.
//
// The handler is called after returning from RequestHandler
// and sending http response. The current connection is passed
// to the handler. The connection is automatically closed after
// returning from the handler.
//
// The server skips calling the handler in the following cases:
//
//     * 'Connection: close' header exists in either request or response.
//     * Unexpected error during response writing to the connection.
//
// The server stops processing requests from hijacked connections.
// Server limits such as Concurrency, ReadTimeout, WriteTimeout, etc.
// aren't applied to hijacked connections.
//
// The handler must not retain references to ctx members.
//
// Arbitrary 'Connection: Upgrade' protocols may be implemented
// with HijackHandler. For instance,
//
//     * WebSocket ( https://en.wikipedia.org/wiki/WebSocket )
//     * HTTP/2.0 ( https://en.wikipedia.org/wiki/HTTP/2 )
//
func (ctx *RequestCtx) Hijack(handler HijackHandler) {
	ctx.hijackHandler = handler
}

// Hijacked returns true after Hijack is called.
func (ctx *RequestCtx) Hijacked() bool {
	return ctx.hijackHandler != nil
}

// SetUserValue stores the given value (arbitrary object)
// under the given key in ctx.
//
// The value stored in ctx may be obtained by UserValue*.
//
// This functionality may be useful for passing arbitrary values between
// functions involved in request processing.
//
// All the values are removed from ctx after returning from the top
// RequestHandler. Additionally, Close method is called on each value
// implementing io.Closer before removing the value from ctx.
func (ctx *RequestCtx) SetUserValue(key string, value interface{}) {
	ctx.userValues.Set(key, value)
}

// SetUserValueBytes stores the given value (arbitrary object)
// under the given key in ctx.
//
// The value stored in ctx may be obtained by UserValue*.
//
// This functionality may be useful for passing arbitrary values between
// functions involved in request processing.
//
// All the values stored in ctx are deleted after returning from RequestHandler.
func (ctx *RequestCtx) SetUserValueBytes(key []byte, value interface{}) {
	ctx.userValues.SetBytes(key, value)
}

// UserValue returns the value stored via SetUserValue* under the given key.
func (ctx *RequestCtx) UserValue(key string) interface{} {
	return ctx.userValues.Get(key)
}

// UserValueBytes returns the value stored via SetUserValue*
// under the given key.
func (ctx *RequestCtx) UserValueBytes(key []byte) interface{} {
	return ctx.userValues.GetBytes(key)
}

// VisitUserValues calls visitor for each existing userValue.
//
// visitor must not retain references to key and value after returning.
// Make key and/or value copies if you need storing them after returning.
func (ctx *RequestCtx) VisitUserValues(visitor func([]byte, interface{})) {
	for i, n := 0, len(ctx.userValues); i < n; i++ {
		kv := &ctx.userValues[i]
		visitor(kv.key, kv.value)
	}
}

type connTLSer interface {
	ConnectionState() tls.ConnectionState
}

// IsTLS returns true if the underlying connection is tls.Conn.
//
// tls.Conn is an encrypted connection (aka SSL, HTTPS).
func (ctx *RequestCtx) IsTLS() bool {
	// cast to (connTLSer) instead of (*tls.Conn), since it catches
	// cases with overridden tls.Conn such as:
	//
	// type customConn struct {
	//     *tls.Conn
	//
	//     // other custom fields here
	// }
	_, ok := ctx.c.(connTLSer)
	return ok
}

// TLSConnectionState returns TLS connection state.
//
// The function returns nil if the underlying connection isn't tls.Conn.
//
// The returned state may be used for verifying TLS version, client certificates,
// etc.
func (ctx *RequestCtx) TLSConnectionState() *tls.ConnectionState {
	tlsConn, ok := ctx.c.(connTLSer)
	if !ok {
		return nil
	}
	state := tlsConn.ConnectionState()
	return &state
}

type firstByteReader struct {
	c        net.Conn
	ch       byte
	byteRead bool
}

func (r *firstByteReader) Read(b []byte) (int, error) {
	if len(b) == 0 {
		return 0, nil
	}
	nn := 0
	if !r.byteRead {
		b[0] = r.ch
		b = b[1:]
		r.byteRead = true
		nn = 1
	}
	n, err := r.c.Read(b)
	return n + nn, err
}

// Logger is used for logging formatted messages.
type Logger interface {
	// Printf must have the same semantics as log.Printf.
	Printf(format string, args ...interface{})
}

var ctxLoggerLock sync.Mutex

type ctxLogger struct {
	ctx    *RequestCtx
	logger Logger
}

func (cl *ctxLogger) Printf(format string, args ...interface{}) {
	ctxLoggerLock.Lock()
	msg := fmt.Sprintf(format, args...)
	ctx := cl.ctx
	cl.logger.Printf("%.3f %s - %s", time.Since(ctx.Time()).Seconds(), ctx.String(), msg)
	ctxLoggerLock.Unlock()
}

var zeroTCPAddr = &net.TCPAddr{
	IP: net.IPv4zero,
}

// String returns unique string representation of the ctx.
//
// The returned value may be useful for logging.
func (ctx *RequestCtx) String() string {
	return fmt.Sprintf("#%016X - %s<->%s - %s %s", ctx.ID(), ctx.LocalAddr(), ctx.RemoteAddr(), ctx.Request.Header.Method(), ctx.URI().FullURI())
}

// ID returns unique ID of the request.
func (ctx *RequestCtx) ID() uint64 {
	return (ctx.connID << 32) | ctx.connRequestNum
}

// ConnID returns unique connection ID.
//
// This ID may be used to match distinct requests to the same incoming
// connection.
func (ctx *RequestCtx) ConnID() uint64 {
	return ctx.connID
}

// Time returns RequestHandler call time truncated to the nearest second.
//
// Call time.Now() at the beginning of RequestHandler in order to obtain
// percise RequestHandler call time.
func (ctx *RequestCtx) Time() time.Time {
	return ctx.time
}

// ConnTime returns the time server starts serving the connection
// the current request came from.
//
// The returned time is truncated to the nearest second.
func (ctx *RequestCtx) ConnTime() time.Time {
	return ctx.connTime
}

// ConnRequestNum returns request sequence number
// for the current connection.
//
// Sequence starts with 1.
func (ctx *RequestCtx) ConnRequestNum() uint64 {
	return ctx.connRequestNum
}

// SetConnectionClose sets 'Connection: close' response header and closes
// connection after the RequestHandler returns.
func (ctx *RequestCtx) SetConnectionClose() {
	ctx.Response.SetConnectionClose()
}

// SetStatusCode sets response status code.
func (ctx *RequestCtx) SetStatusCode(statusCode int) {
	ctx.Response.SetStatusCode(statusCode)
}

// SetContentType sets response Content-Type.
func (ctx *RequestCtx) SetContentType(contentType string) {
	ctx.Response.Header.SetContentType(contentType)
}

// SetContentTypeBytes sets response Content-Type.
//
// It is safe modifying contentType buffer after function return.
func (ctx *RequestCtx) SetContentTypeBytes(contentType []byte) {
	ctx.Response.Header.SetContentTypeBytes(contentType)
}

// RequestURI returns RequestURI.
//
// This uri is valid until returning from RequestHandler.
func (ctx *RequestCtx) RequestURI() []byte {
	return ctx.Request.Header.RequestURI()
}

// URI returns requested uri.
//
// The uri is valid until returning from RequestHandler.
func (ctx *RequestCtx) URI() *URI {
	return ctx.Request.URI()
}

// Referer returns request referer.
//
// The referer is valid until returning from RequestHandler.
func (ctx *RequestCtx) Referer() []byte {
	return ctx.Request.Header.Referer()
}

// UserAgent returns User-Agent header value from the request.
func (ctx *RequestCtx) UserAgent() []byte {
	return ctx.Request.Header.UserAgent()
}

// Path returns requested path.
//
// The path is valid until returning from RequestHandler.
func (ctx *RequestCtx) Path() []byte {
	return ctx.URI().Path()
}

// Host returns requested host.
//
// The host is valid until returning from RequestHandler.
func (ctx *RequestCtx) Host() []byte {
	return ctx.URI().Host()
}

// QueryArgs returns query arguments from RequestURI.
//
// It doesn't return POST'ed arguments - use PostArgs() for this.
//
// Returned arguments are valid until returning from RequestHandler.
//
// See also PostArgs, FormValue and FormFile.
func (ctx *RequestCtx) QueryArgs() *Args {
	return ctx.URI().QueryArgs()
}

// PostArgs returns POST arguments.
//
// It doesn't return query arguments from RequestURI - use QueryArgs for this.
//
// Returned arguments are valid until returning from RequestHandler.
//
// See also QueryArgs, FormValue and FormFile.
func (ctx *RequestCtx) PostArgs() *Args {
	return ctx.Request.PostArgs()
}

// MultipartForm returns requests's multipart form.
//
// Returns ErrNoMultipartForm if request's content-type
// isn't 'multipart/form-data'.
//
// All uploaded temporary files are automatically deleted after
// returning from RequestHandler. Either move or copy uploaded files
// into new place if you want retaining them.
//
// Use SaveMultipartFile function for permanently saving uploaded file.
//
// The returned form is valid until returning from RequestHandler.
//
// See also FormFile and FormValue.
func (ctx *RequestCtx) MultipartForm() (*multipart.Form, error) {
	return ctx.Request.MultipartForm()
}

// FormFile returns uploaded file associated with the given multipart form key.
//
// The file is automatically deleted after returning from RequestHandler,
// so either move or copy uploaded file into new place if you want retaining it.
//
// Use SaveMultipartFile function for permanently saving uploaded file.
//
// The returned file header is valid until returning from RequestHandler.
func (ctx *RequestCtx) FormFile(key string) (*multipart.FileHeader, error) {
	mf, err := ctx.MultipartForm()
	if err != nil {
		return nil, err
	}
	if mf.File == nil {
		return nil, err
	}
	fhh := mf.File[key]
	if fhh == nil {
		return nil, ErrMissingFile
	}
	return fhh[0], nil
}

// ErrMissingFile may be returned from FormFile when the is no uploaded file
// associated with the given multipart form key.
var ErrMissingFile = errors.New("there is no uploaded file associated with the given key")

// SaveMultipartFile saves multipart file fh under the given filename path.
func SaveMultipartFile(fh *multipart.FileHeader, path string) error {
	f, err := fh.Open()
	if err != nil {
		return err
	}
	defer f.Close()

	if ff, ok := f.(*os.File); ok {
		return os.Rename(ff.Name(), path)
	}

	ff, err := os.Create(path)
	if err != nil {
		return err
	}
	defer ff.Close()
	_, err = copyZeroAlloc(ff, f)
	return err
}

// FormValue returns form value associated with the given key.
//
// The value is searched in the following places:
//
//   * Query string.
//   * POST or PUT body.
//
// There are more fine-grained methods for obtaining form values:
//
//   * QueryArgs for obtaining values from query string.
//   * PostArgs for obtaining values from POST or PUT body.
//   * MultipartForm for obtaining values from multipart form.
//   * FormFile for obtaining uploaded files.
//
// The returned value is valid until returning from RequestHandler.
func (ctx *RequestCtx) FormValue(key string) []byte {
	v := ctx.QueryArgs().Peek(key)
	if len(v) > 0 {
		return v
	}
	v = ctx.PostArgs().Peek(key)
	if len(v) > 0 {
		return v
	}
	mf, err := ctx.MultipartForm()
	if err == nil && mf.Value != nil {
		vv := mf.Value[key]
		if len(vv) > 0 {
			return []byte(vv[0])
		}
	}
	return nil
}

// IsGet returns true if request method is GET.
func (ctx *RequestCtx) IsGet() bool {
	return ctx.Request.Header.IsGet()
}

// IsPost returns true if request method is POST.
func (ctx *RequestCtx) IsPost() bool {
	return ctx.Request.Header.IsPost()
}

// IsPut returns true if request method is PUT.
func (ctx *RequestCtx) IsPut() bool {
	return ctx.Request.Header.IsPut()
}

// IsDelete returns true if request method is DELETE.
func (ctx *RequestCtx) IsDelete() bool {
	return ctx.Request.Header.IsDelete()
}

// Method return request method.
//
// Returned value is valid until returning from RequestHandler.
func (ctx *RequestCtx) Method() []byte {
	return ctx.Request.Header.Method()
}

// IsHead returns true if request method is HEAD.
func (ctx *RequestCtx) IsHead() bool {
	return ctx.Request.Header.IsHead()
}

// RemoteAddr returns client address for the given request.
//
// Always returns non-nil result.
func (ctx *RequestCtx) RemoteAddr() net.Addr {
	if ctx.c == nil {
		return zeroTCPAddr
	}
	addr := ctx.c.RemoteAddr()
	if addr == nil {
		return zeroTCPAddr
	}
	return addr
}

// LocalAddr returns server address for the given request.
//
// Always returns non-nil result.
func (ctx *RequestCtx) LocalAddr() net.Addr {
	if ctx.c == nil {
		return zeroTCPAddr
	}
	addr := ctx.c.LocalAddr()
	if addr == nil {
		return zeroTCPAddr
	}
	return addr
}

// RemoteIP returns the client ip the request came from.
//
// Always returns non-nil result.
func (ctx *RequestCtx) RemoteIP() net.IP {
	return addrToIP(ctx.RemoteAddr())
}

// LocalIP returns the server ip the request came to.
//
// Always returns non-nil result.
func (ctx *RequestCtx) LocalIP() net.IP {
	return addrToIP(ctx.LocalAddr())
}

func addrToIP(addr net.Addr) net.IP {
	x, ok := addr.(*net.TCPAddr)
	if !ok {
		return net.IPv4zero
	}
	return x.IP
}

// Error sets response status code to the given value and sets response body
// to the given message.
func (ctx *RequestCtx) Error(msg string, statusCode int) {
	ctx.Response.Reset()
	ctx.SetStatusCode(statusCode)
	ctx.SetContentTypeBytes(defaultContentType)
	ctx.SetBodyString(msg)
}

// Success sets response Content-Type and body to the given values.
func (ctx *RequestCtx) Success(contentType string, body []byte) {
	ctx.SetContentType(contentType)
	ctx.SetBody(body)
}

// SuccessString sets response Content-Type and body to the given values.
func (ctx *RequestCtx) SuccessString(contentType, body string) {
	ctx.SetContentType(contentType)
	ctx.SetBodyString(body)
}

// Redirect sets 'Location: uri' response header and sets the given statusCode.
//
// statusCode must have one of the following values:
//
//    * StatusMovedPermanently (301)
//    * StatusFound (302)
//    * StatusSeeOther (303)
//    * StatusTemporaryRedirect (307)
//
// All other statusCode values are replaced by StatusFound (302).
//
// The redirect uri may be either absolute or relative to the current
// request uri.
func (ctx *RequestCtx) Redirect(uri string, statusCode int) {
	u := AcquireURI()
	ctx.URI().CopyTo(u)
	u.Update(uri)
	ctx.redirect(u.FullURI(), statusCode)
	ReleaseURI(u)
}

// RedirectBytes sets 'Location: uri' response header and sets
// the given statusCode.
//
// statusCode must have one of the following values:
//
//    * StatusMovedPermanently (301)
//    * StatusFound (302)
//    * StatusSeeOther (303)
//    * StatusTemporaryRedirect (307)
//
// All other statusCode values are replaced by StatusFound (302).
//
// The redirect uri may be either absolute or relative to the current
// request uri.
func (ctx *RequestCtx) RedirectBytes(uri []byte, statusCode int) {
	s := b2s(uri)
	ctx.Redirect(s, statusCode)
}

func (ctx *RequestCtx) redirect(uri []byte, statusCode int) {
	ctx.Response.Header.SetCanonical(strLocation, uri)
	statusCode = getRedirectStatusCode(statusCode)
	ctx.Response.SetStatusCode(statusCode)
}

func getRedirectStatusCode(statusCode int) int {
	if statusCode == StatusMovedPermanently || statusCode == StatusFound ||
		statusCode == StatusSeeOther || statusCode == StatusTemporaryRedirect {
		return statusCode
	}
	return StatusFound
}

// SetBody sets response body to the given value.
//
// It is safe re-using body argument after the function returns.
func (ctx *RequestCtx) SetBody(body []byte) {
	ctx.Response.SetBody(body)
}

// SetBodyString sets response body to the given value.
func (ctx *RequestCtx) SetBodyString(body string) {
	ctx.Response.SetBodyString(body)
}

// ResetBody resets response body contents.
func (ctx *RequestCtx) ResetBody() {
	ctx.Response.ResetBody()
}

// SendFile sends local file contents from the given path as response body.
//
// This is a shortcut to ServeFile(ctx, path).
//
// SendFile logs all the errors via ctx.Logger.
//
// See also ServeFile, FSHandler and FS.
func (ctx *RequestCtx) SendFile(path string) {
	ServeFile(ctx, path)
}

// SendFileBytes sends local file contents from the given path as response body.
//
// This is a shortcut to ServeFileBytes(ctx, path).
//
// SendFileBytes logs all the errors via ctx.Logger.
//
// See also ServeFileBytes, FSHandler and FS.
func (ctx *RequestCtx) SendFileBytes(path []byte) {
	ServeFileBytes(ctx, path)
}

// IfModifiedSince returns true if lastModified exceeds 'If-Modified-Since'
// value from the request header.
//
// The function returns true also 'If-Modified-Since' request header is missing.
func (ctx *RequestCtx) IfModifiedSince(lastModified time.Time) bool {
	ifModStr := ctx.Request.Header.peek(strIfModifiedSince)
	if len(ifModStr) == 0 {
		return true
	}
	ifMod, err := ParseHTTPDate(ifModStr)
	if err != nil {
		return true
	}
	lastModified = lastModified.Truncate(time.Second)
	return ifMod.Before(lastModified)
}

// NotModified resets response and sets '304 Not Modified' response status code.
func (ctx *RequestCtx) NotModified() {
	ctx.Response.Reset()
	ctx.SetStatusCode(StatusNotModified)
}

// NotFound resets response and sets '404 Not Found' response status code.
func (ctx *RequestCtx) NotFound() {
	ctx.Response.Reset()
	ctx.SetStatusCode(StatusNotFound)
	ctx.SetBodyString("404 Page not found")
}

// Write writes p into response body.
func (ctx *RequestCtx) Write(p []byte) (int, error) {
	ctx.Response.AppendBody(p)
	return len(p), nil
}

// WriteString appends s to response body.
func (ctx *RequestCtx) WriteString(s string) (int, error) {
	ctx.Response.AppendBodyString(s)
	return len(s), nil
}

// PostBody returns POST request body.
//
// The returned value is valid until RequestHandler return.
func (ctx *RequestCtx) PostBody() []byte {
	return ctx.Request.Body()
}

// SetBodyStream sets response body stream and, optionally body size.
//
// bodyStream.Close() is called after finishing reading all body data
// if it implements io.Closer.
//
// If bodySize is >= 0, then bodySize bytes must be provided by bodyStream
// before returning io.EOF.
//
// If bodySize < 0, then bodyStream is read until io.EOF.
//
// See also SetBodyStreamWriter.
func (ctx *RequestCtx) SetBodyStream(bodyStream io.Reader, bodySize int) {
	ctx.Response.SetBodyStream(bodyStream, bodySize)
}

// SetBodyStreamWriter registers the given stream writer for populating
// response body.
//
// Access to RequestCtx and/or its' members is forbidden from sw.
//
// This function may be used in the following cases:
//
//     * if response body is too big (more than 10MB).
//     * if response body is streamed from slow external sources.
//     * if response body must be streamed to the client in chunks.
//     (aka `http server push`).
func (ctx *RequestCtx) SetBodyStreamWriter(sw StreamWriter) {
	ctx.Response.SetBodyStreamWriter(sw)
}

// IsBodyStream returns true if response body is set via SetBodyStream*.
func (ctx *RequestCtx) IsBodyStream() bool {
	return ctx.Response.IsBodyStream()
}

// Logger returns logger, which may be used for logging arbitrary
// request-specific messages inside RequestHandler.
//
// Each message logged via returned logger contains request-specific information
// such as request id, request duration, local address, remote address,
// request method and request url.
//
// It is safe re-using returned logger for logging multiple messages
// for the current request.
//
// The returned logger is valid until returning from RequestHandler.
func (ctx *RequestCtx) Logger() Logger {
	if ctx.logger.ctx == nil {
		ctx.logger.ctx = ctx
	}
	if ctx.logger.logger == nil {
		ctx.logger.logger = ctx.s.logger()
	}
	return &ctx.logger
}

// TimeoutError sets response status code to StatusRequestTimeout and sets
// body to the given msg.
//
// All response modifications after TimeoutError call are ignored.
//
// TimeoutError MUST be called before returning from RequestHandler if there are
// references to ctx and/or its members in other goroutines remain.
//
// Usage of this function is discouraged. Prefer eliminating ctx references
// from pending goroutines instead of using this function.
func (ctx *RequestCtx) TimeoutError(msg string) {
	ctx.TimeoutErrorWithCode(msg, StatusRequestTimeout)
}

// TimeoutErrorWithCode sets response body to msg and response status
// code to statusCode.
//
// All response modifications after TimeoutErrorWithCode call are ignored.
//
// TimeoutErrorWithCode MUST be called before returning from RequestHandler
// if there are references to ctx and/or its members in other goroutines remain.
//
// Usage of this function is discouraged. Prefer eliminating ctx references
// from pending goroutines instead of using this function.
func (ctx *RequestCtx) TimeoutErrorWithCode(msg string, statusCode int) {
	var resp Response
	resp.SetStatusCode(statusCode)
	resp.SetBodyString(msg)
	ctx.TimeoutErrorWithResponse(&resp)
}

// TimeoutErrorWithResponse marks the ctx as timed out and sends the given
// response to the client.
//
// All ctx modifications after TimeoutErrorWithResponse call are ignored.
//
// TimeoutErrorWithResponse MUST be called before returning from RequestHandler
// if there are references to ctx and/or its members in other goroutines remain.
//
// Usage of this function is discouraged. Prefer eliminating ctx references
// from pending goroutines instead of using this function.
func (ctx *RequestCtx) TimeoutErrorWithResponse(resp *Response) {
	respCopy := &Response{}
	resp.CopyTo(respCopy)
	ctx.timeoutResponse = respCopy
}

// ListenAndServe serves HTTP requests from the given TCP4 addr.
//
// Pass custom listener to Serve if you need listening on non-TCP4 media
// such as IPv6.
func (s *Server) ListenAndServe(addr string) error {
	ln, err := net.Listen("tcp4", addr)
	if err != nil {
		return err
	}
	return s.Serve(ln)
}

// ListenAndServeUNIX serves HTTP requests from the given UNIX addr.
//
// The function deletes existing file at addr before starting serving.
//
// The server sets the given file mode for the UNIX addr.
func (s *Server) ListenAndServeUNIX(addr string, mode os.FileMode) error {
	if err := os.Remove(addr); err != nil && !os.IsNotExist(err) {
		return fmt.Errorf("unexpected error when trying to remove unix socket file %q: %s", addr, err)
	}
	ln, err := net.Listen("unix", addr)
	if err != nil {
		return err
	}
	if err = os.Chmod(addr, mode); err != nil {
		return fmt.Errorf("cannot chmod %#o for %q: %s", mode, addr, err)
	}
	return s.Serve(ln)
}

// ListenAndServeTLS serves HTTPS requests from the given TCP4 addr.
//
// certFile and keyFile are paths to TLS certificate and key files.
//
// Pass custom listener to Serve if you need listening on non-TCP4 media
// such as IPv6.
func (s *Server) ListenAndServeTLS(addr, certFile, keyFile string) error {
	ln, err := net.Listen("tcp4", addr)
	if err != nil {
		return err
	}
	return s.ServeTLS(ln, certFile, keyFile)
}

// ListenAndServeTLSEmbed serves HTTPS requests from the given TCP4 addr.
//
// certData and keyData must contain valid TLS certificate and key data.
//
// Pass custom listener to Serve if you need listening on arbitrary media
// such as IPv6.
func (s *Server) ListenAndServeTLSEmbed(addr string, certData, keyData []byte) error {
	ln, err := net.Listen("tcp4", addr)
	if err != nil {
		return err
	}
	return s.ServeTLSEmbed(ln, certData, keyData)
}

// ServeTLS serves HTTPS requests from the given listener.
//
// certFile and keyFile are paths to TLS certificate and key files.
func (s *Server) ServeTLS(ln net.Listener, certFile, keyFile string) error {
	lnTLS, err := newTLSListener(ln, certFile, keyFile)
	if err != nil {
		return err
	}
	return s.Serve(lnTLS)
}

// ServeTLSEmbed serves HTTPS requests from the given listener.
//
// certData and keyData must contain valid TLS certificate and key data.
func (s *Server) ServeTLSEmbed(ln net.Listener, certData, keyData []byte) error {
	lnTLS, err := newTLSListenerEmbed(ln, certData, keyData)
	if err != nil {
		return err
	}
	return s.Serve(lnTLS)
}

func newTLSListener(ln net.Listener, certFile, keyFile string) (net.Listener, error) {
	cert, err := tls.LoadX509KeyPair(certFile, keyFile)
	if err != nil {
		return nil, fmt.Errorf("cannot load TLS key pair from certFile=%q and keyFile=%q: %s", certFile, keyFile, err)
	}
	return newCertListener(ln, &cert), nil
}

func newTLSListenerEmbed(ln net.Listener, certData, keyData []byte) (net.Listener, error) {
	cert, err := tls.X509KeyPair(certData, keyData)
	if err != nil {
		return nil, fmt.Errorf("cannot load TLS key pair from the provided certData(%d) and keyData(%d): %s",
			len(certData), len(keyData), err)
	}
	return newCertListener(ln, &cert), nil
}

func newCertListener(ln net.Listener, cert *tls.Certificate) net.Listener {
	tlsConfig := &tls.Config{
		Certificates:             []tls.Certificate{*cert},
		PreferServerCipherSuites: true,
	}
	return tls.NewListener(ln, tlsConfig)
}

// DefaultConcurrency is the maximum number of concurrent connections
// the Server may serve by default (i.e. if Server.Concurrency isn't set).
const DefaultConcurrency = 256 * 1024

// Serve serves incoming connections from the given listener.
//
// Serve blocks until the given listener returns permanent error.
func (s *Server) Serve(ln net.Listener) error {
	var lastOverflowErrorTime time.Time
	var lastPerIPErrorTime time.Time
	var c net.Conn
	var err error

	maxWorkersCount := s.getConcurrency()
	s.concurrencyCh = make(chan struct{}, maxWorkersCount)
	wp := &workerPool{
		WorkerFunc:      s.serveConn,
		MaxWorkersCount: maxWorkersCount,
		LogAllErrors:    s.LogAllErrors,
		Logger:          s.logger(),
	}
	wp.Start()

	for {
		if c, err = acceptConn(s, ln, &lastPerIPErrorTime); err != nil {
			wp.Stop()
			if err == io.EOF {
				return nil
			}
			return err
		}
		if !wp.Serve(c) {
			s.writeFastError(c, StatusServiceUnavailable,
				"The connection cannot be served because Server.Concurrency limit exceeded")
			c.Close()
			if time.Since(lastOverflowErrorTime) > time.Minute {
				s.logger().Printf("The incoming connection cannot be served, because %d concurrent connections are served. "+
					"Try increasing Server.Concurrency", maxWorkersCount)
				lastOverflowErrorTime = CoarseTimeNow()
			}

			// The current server reached concurrency limit,
			// so give other concurrently running servers a chance
			// accepting incoming connections on the same address.
			//
			// There is a hope other servers didn't reach their
			// concurrency limits yet :)
			time.Sleep(100 * time.Millisecond)
		}
		c = nil
	}
}

func acceptConn(s *Server, ln net.Listener, lastPerIPErrorTime *time.Time) (net.Conn, error) {
	for {
		c, err := ln.Accept()
		if err != nil {
			if c != nil {
				panic("BUG: net.Listener returned non-nil conn and non-nil error")
			}
			if netErr, ok := err.(net.Error); ok && netErr.Temporary() {
				s.logger().Printf("Temporary error when accepting new connections: %s", netErr)
				time.Sleep(time.Second)
				continue
			}
			if err != io.EOF && !strings.Contains(err.Error(), "use of closed network connection") {
				s.logger().Printf("Permanent error when accepting new connections: %s", err)
				return nil, err
			}
			return nil, io.EOF
		}
		if c == nil {
			panic("BUG: net.Listener returned (nil, nil)")
		}
		if s.MaxConnsPerIP > 0 {
			pic := wrapPerIPConn(s, c)
			if pic == nil {
				if time.Since(*lastPerIPErrorTime) > time.Minute {
					s.logger().Printf("The number of connections from %s exceeds MaxConnsPerIP=%d",
						getConnIP4(c), s.MaxConnsPerIP)
					*lastPerIPErrorTime = CoarseTimeNow()
				}
				continue
			}
			c = pic
		}
		return c, nil
	}
}

func wrapPerIPConn(s *Server, c net.Conn) net.Conn {
	ip := getUint32IP(c)
	if ip == 0 {
		return c
	}
	n := s.perIPConnCounter.Register(ip)
	if n > s.MaxConnsPerIP {
		s.perIPConnCounter.Unregister(ip)
		s.writeFastError(c, StatusTooManyRequests, "The number of connections from your ip exceeds MaxConnsPerIP")
		c.Close()
		return nil
	}
	return acquirePerIPConn(c, ip, &s.perIPConnCounter)
}

var defaultLogger = Logger(log.New(os.Stderr, "", log.LstdFlags))

func (s *Server) logger() Logger {
	if s.Logger != nil {
		return s.Logger
	}
	return defaultLogger
}

var (
	// ErrPerIPConnLimit may be returned from ServeConn if the number of connections
	// per ip exceeds Server.MaxConnsPerIP.
	ErrPerIPConnLimit = errors.New("too many connections per ip")

	// ErrConcurrencyLimit may be returned from ServeConn if the number
	// of concurrenty served connections exceeds Server.Concurrency.
	ErrConcurrencyLimit = errors.New("canot serve the connection because Server.Concurrency concurrent connections are served")

	// ErrKeepaliveTimeout is returned from ServeConn
	// if the connection lifetime exceeds MaxKeepaliveDuration.
	ErrKeepaliveTimeout = errors.New("exceeded MaxKeepaliveDuration")
)

// ServeConn serves HTTP requests from the given connection.
//
// ServeConn returns nil if all requests from the c are successfully served.
// It returns non-nil error otherwise.
//
// Connection c must immediately propagate all the data passed to Write()
// to the client. Otherwise requests' processing may hang.
//
// ServeConn closes c before returning.
func (s *Server) ServeConn(c net.Conn) error {
	if s.MaxConnsPerIP > 0 {
		pic := wrapPerIPConn(s, c)
		if pic == nil {
			return ErrPerIPConnLimit
		}
		c = pic
	}

	n := atomic.AddUint32(&s.concurrency, 1)
	if n > uint32(s.getConcurrency()) {
		atomic.AddUint32(&s.concurrency, ^uint32(0))
		s.writeFastError(c, StatusServiceUnavailable, "The connection cannot be served because Server.Concurrency limit exceeded")
		c.Close()
		return ErrConcurrencyLimit
	}

	err := s.serveConn(c)

	atomic.AddUint32(&s.concurrency, ^uint32(0))

	if err != errHijacked {
		err1 := c.Close()
		if err == nil {
			err = err1
		}
	} else {
		err = nil
	}
	return err
}

var errHijacked = errors.New("connection has been hijacked")

func (s *Server) getConcurrency() int {
	n := s.Concurrency
	if n <= 0 {
		n = DefaultConcurrency
	}
	return n
}

var globalConnID uint64

func nextConnID() uint64 {
	return atomic.AddUint64(&globalConnID, 1)
}

// DefaultMaxRequestBodySize is the maximum request body size the server
// reads by default.
//
// See Server.MaxRequestBodySize for details.
const DefaultMaxRequestBodySize = 4 * 1024 * 1024

func (s *Server) serveConn(c net.Conn) error {
	serverName := s.getServerName()
	connRequestNum := uint64(0)
	connID := nextConnID()
	currentTime := CoarseTimeNow()
	connTime := currentTime
	maxRequestBodySize := s.MaxRequestBodySize
	if maxRequestBodySize <= 0 {
		maxRequestBodySize = DefaultMaxRequestBodySize
	}

	ctx := s.acquireCtx(c)
	ctx.connTime = connTime
	isTLS := ctx.IsTLS()
	var (
		br *bufio.Reader
		bw *bufio.Writer

		err             error
		timeoutResponse *Response
		hijackHandler   HijackHandler

		lastReadDeadlineTime  time.Time
		lastWriteDeadlineTime time.Time

		connectionClose bool
		isHTTP11        bool
	)
	for {
		connRequestNum++
		ctx.time = currentTime

		if s.ReadTimeout > 0 || s.MaxKeepaliveDuration > 0 {
			lastReadDeadlineTime = s.updateReadDeadline(c, ctx, lastReadDeadlineTime)
			if lastReadDeadlineTime.IsZero() {
				err = ErrKeepaliveTimeout
				break
			}
		}

		if !(s.ReduceMemoryUsage || ctx.lastReadDuration > time.Second) || br != nil {
			if br == nil {
				br = acquireReader(ctx)
			}
		} else {
			br, err = acquireByteReader(&ctx)
		}
		ctx.Request.isTLS = isTLS

		if err == nil {
			if s.DisableHeaderNamesNormalizing {
				ctx.Request.Header.DisableNormalizing()
				ctx.Response.Header.DisableNormalizing()
			}
			err = ctx.Request.readLimitBody(br, maxRequestBodySize, s.GetOnly)
			if br.Buffered() == 0 || err != nil {
				releaseReader(s, br)
				br = nil
			}
		}

		currentTime = CoarseTimeNow()
		ctx.lastReadDuration = currentTime.Sub(ctx.time)

		if err != nil {
			if err == io.EOF {
				err = nil
			} else {
				bw = writeErrorResponse(bw, ctx, err)
			}
			break
		}

		// 'Expect: 100-continue' request handling.
		// See http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html for details.
		if !ctx.Request.Header.noBody() && ctx.Request.MayContinue() {
			// Send 'HTTP/1.1 100 Continue' response.
			if bw == nil {
				bw = acquireWriter(ctx)
			}
			bw.Write(strResponseContinue)
			err = bw.Flush()
			releaseWriter(s, bw)
			bw = nil
			if err != nil {
				break
			}

			// Read request body.
			if br == nil {
				br = acquireReader(ctx)
			}
			err = ctx.Request.ContinueReadBody(br, maxRequestBodySize)
			if br.Buffered() == 0 || err != nil {
				releaseReader(s, br)
				br = nil
			}
			if err != nil {
				bw = writeErrorResponse(bw, ctx, err)
				break
			}
		}

		connectionClose = s.DisableKeepalive || ctx.Request.Header.connectionCloseFast()
		isHTTP11 = ctx.Request.Header.IsHTTP11()

		ctx.Response.Header.SetServerBytes(serverName)
		ctx.connID = connID
		ctx.connRequestNum = connRequestNum
		ctx.connTime = connTime
		ctx.time = currentTime
		s.Handler(ctx)

		timeoutResponse = ctx.timeoutResponse
		if timeoutResponse != nil {
			ctx = s.acquireCtx(c)
			timeoutResponse.CopyTo(&ctx.Response)
			if br != nil {
				// Close connection, since br may be attached to the old ctx via ctx.fbr.
				ctx.SetConnectionClose()
			}
		}

		if !ctx.IsGet() && ctx.IsHead() {
			ctx.Response.SkipBody = true
		}
		ctx.Request.Reset()

		hijackHandler = ctx.hijackHandler
		ctx.hijackHandler = nil

		ctx.userValues.Reset()

		if s.MaxRequestsPerConn > 0 && connRequestNum >= uint64(s.MaxRequestsPerConn) {
			ctx.SetConnectionClose()
		}

		if s.WriteTimeout > 0 || s.MaxKeepaliveDuration > 0 {
			lastWriteDeadlineTime = s.updateWriteDeadline(c, ctx, lastWriteDeadlineTime)
		}

		// Verify Request.Header.connectionCloseFast() again,
		// since request handler might trigger full headers' parsing.
		connectionClose = connectionClose || ctx.Request.Header.connectionCloseFast() || ctx.Response.ConnectionClose()
		if connectionClose {
			ctx.Response.Header.SetCanonical(strConnection, strClose)
		} else if !isHTTP11 {
			// Set 'Connection: keep-alive' response header for non-HTTP/1.1 request.
			// There is no need in setting this header for http/1.1, since in http/1.1
			// connections are keep-alive by default.
			ctx.Response.Header.SetCanonical(strConnection, strKeepAlive)
		}

		if len(ctx.Response.Header.Server()) == 0 {
			ctx.Response.Header.SetServerBytes(serverName)
		}

		if bw == nil {
			bw = acquireWriter(ctx)
		}
		if err = writeResponse(ctx, bw); err != nil {
			break
		}

		if br == nil || connectionClose {
			err = bw.Flush()
			releaseWriter(s, bw)
			bw = nil
			if err != nil {
				break
			}
			if connectionClose {
				break
			}
		}

		if hijackHandler != nil {
			var hjr io.Reader
			hjr = c
			if br != nil {
				hjr = br
				br = nil

				// br may point to ctx.fbr, so do not return ctx into pool.
				ctx = s.acquireCtx(c)
			}
			if bw != nil {
				err = bw.Flush()
				releaseWriter(s, bw)
				bw = nil
				if err != nil {
					break
				}
			}
			c.SetReadDeadline(zeroTime)
			c.SetWriteDeadline(zeroTime)
			go hijackConnHandler(hjr, c, s, hijackHandler)
			hijackHandler = nil
			err = errHijacked
			break
		}

		currentTime = CoarseTimeNow()
	}

	if br != nil {
		releaseReader(s, br)
	}
	if bw != nil {
		releaseWriter(s, bw)
	}
	s.releaseCtx(ctx)
	return err
}

func (s *Server) updateReadDeadline(c net.Conn, ctx *RequestCtx, lastDeadlineTime time.Time) time.Time {
	readTimeout := s.ReadTimeout
	currentTime := ctx.time
	if s.MaxKeepaliveDuration > 0 {
		connTimeout := s.MaxKeepaliveDuration - currentTime.Sub(ctx.connTime)
		if connTimeout <= 0 {
			return zeroTime
		}
		if connTimeout < readTimeout {
			readTimeout = connTimeout
		}
	}

	// Optimization: update read deadline only if more than 25%
	// of the last read deadline exceeded.
	// See https://github.com/golang/go/issues/15133 for details.
	if currentTime.Sub(lastDeadlineTime) > (readTimeout >> 2) {
		if err := c.SetReadDeadline(currentTime.Add(readTimeout)); err != nil {
			panic(fmt.Sprintf("BUG: error in SetReadDeadline(%s): %s", readTimeout, err))
		}
		lastDeadlineTime = currentTime
	}
	return lastDeadlineTime
}

func (s *Server) updateWriteDeadline(c net.Conn, ctx *RequestCtx, lastDeadlineTime time.Time) time.Time {
	writeTimeout := s.WriteTimeout
	if s.MaxKeepaliveDuration > 0 {
		connTimeout := s.MaxKeepaliveDuration - time.Since(ctx.connTime)
		if connTimeout <= 0 {
			// MaxKeepAliveDuration exceeded, but let's try sending response anyway
			// in 100ms with 'Connection: close' header.
			ctx.SetConnectionClose()
			connTimeout = 100 * time.Millisecond
		}
		if connTimeout < writeTimeout {
			writeTimeout = connTimeout
		}
	}

	// Optimization: update write deadline only if more than 25%
	// of the last write deadline exceeded.
	// See https://github.com/golang/go/issues/15133 for details.
	currentTime := CoarseTimeNow()
	if currentTime.Sub(lastDeadlineTime) > (writeTimeout >> 2) {
		if err := c.SetWriteDeadline(currentTime.Add(writeTimeout)); err != nil {
			panic(fmt.Sprintf("BUG: error in SetWriteDeadline(%s): %s", writeTimeout, err))
		}
		lastDeadlineTime = currentTime
	}
	return lastDeadlineTime
}

func hijackConnHandler(r io.Reader, c net.Conn, s *Server, h HijackHandler) {
	hjc := s.acquireHijackConn(r, c)
	h(hjc)

	if br, ok := r.(*bufio.Reader); ok {
		releaseReader(s, br)
	}
	c.Close()
	s.releaseHijackConn(hjc)
}

func (s *Server) acquireHijackConn(r io.Reader, c net.Conn) *hijackConn {
	v := s.hijackConnPool.Get()
	if v == nil {
		hjc := &hijackConn{
			Conn: c,
			r:    r,
		}
		return hjc
	}
	hjc := v.(*hijackConn)
	hjc.Conn = c
	hjc.r = r
	return hjc
}

func (s *Server) releaseHijackConn(hjc *hijackConn) {
	hjc.Conn = nil
	hjc.r = nil
	s.hijackConnPool.Put(hjc)
}

type hijackConn struct {
	net.Conn
	r io.Reader
}

func (c hijackConn) Read(p []byte) (int, error) {
	return c.r.Read(p)
}

func (c hijackConn) Close() error {
	// hijacked conn is closed in hijackConnHandler.
	return nil
}

// LastTimeoutErrorResponse returns the last timeout response set
// via TimeoutError* call.
//
// This function is intended for custom server implementations.
func (ctx *RequestCtx) LastTimeoutErrorResponse() *Response {
	return ctx.timeoutResponse
}

func writeResponse(ctx *RequestCtx, w *bufio.Writer) error {
	if ctx.timeoutResponse != nil {
		panic("BUG: cannot write timed out response")
	}
	err := ctx.Response.Write(w)
	ctx.Response.Reset()
	return err
}

const (
	defaultReadBufferSize  = 4096
	defaultWriteBufferSize = 4096
)

func acquireByteReader(ctxP **RequestCtx) (*bufio.Reader, error) {
	ctx := *ctxP
	s := ctx.s
	c := ctx.c
	t := ctx.time
	s.releaseCtx(ctx)

	// Make GC happy, so it could garbage collect ctx
	// while we waiting for the next request.
	ctx = nil
	*ctxP = nil

	v := s.bytePool.Get()
	if v == nil {
		v = make([]byte, 1)
	}
	b := v.([]byte)
	n, err := c.Read(b)
	ch := b[0]
	s.bytePool.Put(v)
	ctx = s.acquireCtx(c)
	ctx.time = t
	*ctxP = ctx
	if err != nil {
		// Treat all errors as EOF on unsuccessful read
		// of the first request byte.
		return nil, io.EOF
	}
	if n != 1 {
		panic("BUG: Reader must return at least one byte")
	}

	ctx.fbr.c = c
	ctx.fbr.ch = ch
	ctx.fbr.byteRead = false
	r := acquireReader(ctx)
	r.Reset(&ctx.fbr)
	return r, nil
}

func acquireReader(ctx *RequestCtx) *bufio.Reader {
	v := ctx.s.readerPool.Get()
	if v == nil {
		n := ctx.s.ReadBufferSize
		if n <= 0 {
			n = defaultReadBufferSize
		}
		return bufio.NewReaderSize(ctx.c, n)
	}
	r := v.(*bufio.Reader)
	r.Reset(ctx.c)
	return r
}

func releaseReader(s *Server, r *bufio.Reader) {
	s.readerPool.Put(r)
}

func acquireWriter(ctx *RequestCtx) *bufio.Writer {
	v := ctx.s.writerPool.Get()
	if v == nil {
		n := ctx.s.WriteBufferSize
		if n <= 0 {
			n = defaultWriteBufferSize
		}
		return bufio.NewWriterSize(ctx.c, n)
	}
	w := v.(*bufio.Writer)
	w.Reset(ctx.c)
	return w
}

func releaseWriter(s *Server, w *bufio.Writer) {
	s.writerPool.Put(w)
}

func (s *Server) acquireCtx(c net.Conn) *RequestCtx {
	v := s.ctxPool.Get()
	var ctx *RequestCtx
	if v == nil {
		ctx = &RequestCtx{
			s: s,
		}
		keepBodyBuffer := !s.ReduceMemoryUsage
		ctx.Request.keepBodyBuffer = keepBodyBuffer
		ctx.Response.keepBodyBuffer = keepBodyBuffer
	} else {
		ctx = v.(*RequestCtx)
	}
	ctx.c = c
	return ctx
}

// Init2 prepares ctx for passing to RequestHandler.
//
// conn is used only for determining local and remote addresses.
//
// This function is intended for custom Server implementations.
// See https://github.com/valyala/httpteleport for details.
func (ctx *RequestCtx) Init2(conn net.Conn, logger Logger, reduceMemoryUsage bool) {
	ctx.c = conn
	ctx.logger.logger = logger
	ctx.connID = nextConnID()
	ctx.s = fakeServer
	ctx.connRequestNum = 0
	ctx.connTime = CoarseTimeNow()
	ctx.time = ctx.connTime

	keepBodyBuffer := !reduceMemoryUsage
	ctx.Request.keepBodyBuffer = keepBodyBuffer
	ctx.Response.keepBodyBuffer = keepBodyBuffer
}

// Init prepares ctx for passing to RequestHandler.
//
// remoteAddr and logger are optional. They are used by RequestCtx.Logger().
//
// This function is intended for custom Server implementations.
func (ctx *RequestCtx) Init(req *Request, remoteAddr net.Addr, logger Logger) {
	if remoteAddr == nil {
		remoteAddr = zeroTCPAddr
	}
	c := &fakeAddrer{
		laddr: zeroTCPAddr,
		raddr: remoteAddr,
	}
	if logger == nil {
		logger = defaultLogger
	}
	ctx.Init2(c, logger, true)
	req.CopyTo(&ctx.Request)
}

var fakeServer = &Server{
	// Initialize concurrencyCh for TimeoutHandler
	concurrencyCh: make(chan struct{}, DefaultConcurrency),
}

type fakeAddrer struct {
	net.Conn
	laddr net.Addr
	raddr net.Addr
}

func (fa *fakeAddrer) RemoteAddr() net.Addr {
	return fa.raddr
}

func (fa *fakeAddrer) LocalAddr() net.Addr {
	return fa.laddr
}

func (fa *fakeAddrer) Read(p []byte) (int, error) {
	panic("BUG: unexpected Read call")
}

func (fa *fakeAddrer) Write(p []byte) (int, error) {
	panic("BUG: unexpected Write call")
}

func (fa *fakeAddrer) Close() error {
	panic("BUG: unexpected Close call")
}

func (s *Server) releaseCtx(ctx *RequestCtx) {
	if ctx.timeoutResponse != nil {
		panic("BUG: cannot release timed out RequestCtx")
	}
	ctx.c = nil
	ctx.fbr.c = nil
	s.ctxPool.Put(ctx)
}

func (s *Server) getServerName() []byte {
	v := s.serverName.Load()
	var serverName []byte
	if v == nil {
		serverName = []byte(s.Name)
		if len(serverName) == 0 {
			serverName = defaultServerName
		}
		s.serverName.Store(serverName)
	} else {
		serverName = v.([]byte)
	}
	return serverName
}

func (s *Server) writeFastError(w io.Writer, statusCode int, msg string) {
	w.Write(statusLine(statusCode))
	fmt.Fprintf(w, "Connection: close\r\n"+
		"Server: %s\r\n"+
		"Date: %s\r\n"+
		"Content-Type: text/plain\r\n"+
		"Content-Length: %d\r\n"+
		"\r\n"+
		"%s",
		s.getServerName(), serverDate.Load(), len(msg), msg)
}

func writeErrorResponse(bw *bufio.Writer, ctx *RequestCtx, err error) *bufio.Writer {
	if _, ok := err.(*ErrSmallBuffer); ok {
		ctx.Error("Too big request header", StatusRequestHeaderFieldsTooLarge)
	} else {
		ctx.Error("Error when parsing request", StatusBadRequest)
	}
	ctx.SetConnectionClose()
	if bw == nil {
		bw = acquireWriter(ctx)
	}
	writeResponse(ctx, bw)
	bw.Flush()
	return bw
}


text:
<?php

declare(strict_types=1);

namespace Talesweaver\Application\Command\Positionable\UpdateMultiple;

use Assert\Assertion;
use Exception;
use Talesweaver\Domain\Author;
use Talesweaver\Domain\Security\AuthorAccessInterface;

final class Command implements AuthorAccessInterface
{
    /**
     * @var DTO[]
     */
    private $items;

    /**
     * @var Author
     */
    private $author;

    public function __construct(array $items)
    {
        Assertion::allIsInstanceOf($items, DTO::class);

        $this->author = $this->getAuthor($items);
        $this->items = $items;
    }

    public function getItems(): array
    {
        return $this->items;
    }

    public function isAllowed(Author $author): bool
    {
        return $author === $this->author;
    }

    private function getAuthor(array $items): Author
    {
        $author = array_reduce(
            $items,
            function (?Author $accumulator, DTO $dto): ?Author {
                $author = $dto->getPositionable()->getCreatedBy();
                if (null !== $accumulator && $author !== $accumulator) {
                    throw new Exception(sprintf(
                        'Tried to change position for items of class "%s" of two different authors: "%s" and "%s"',
                        get_class($dto->getPositionable()),
                        $accumulator->getId()->toString(),
                        $author->getId()->toString()
                    ));
                }

                if (null === $accumulator) {
                    $accumulator = $author;
                }

                return $accumulator;
            }
        );

        Assertion::notNull($author, "No user supplied!");
        return $author;
    }
}


text:
const net = require('net');
const express = require('express');
const logger = require('../../lib/logger').test;

const generateBackendServer = port => {
  const app = express();

  app.all('*', (req, res) => {
    const port = req.connection.server.address().port;
    res.send('Hello from port ' + port);
  });
  return new Promise((resolve) => {
    const runningApp = app.listen(port || 0, () => {
      logger.log('running test stub server at ' + runningApp.address().port);
      resolve({
        app: runningApp
      });
    });
  });
};

const findOpenPortNumbers = function (count = 1) {
  let completeCount = 0;
  const ports = [];
  return new Promise((resolve, reject) => {
    for (let i = 0; i < count; i++) {
      const server = net.createServer();

      server.listen(0);

      server.on('listening', () => {
        ports.push(server.address().port);

        server.once('close', () => {
          completeCount++;

          if (completeCount === count) {
            resolve(ports);
          }
        });
        server.close();
      });

      server.on('error', (err) => {
        reject(err);
      });
    }
  });
};

module.exports = {
  generateBackendServer,
  findOpenPortNumbers
};