github.com/vonwenm/spdy@v0.0.0-20151107123324-431b911c6217/client.go (about)

     1  // Copyright 2013 Jamie Hall. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package spdy
     6  
     7  import (
     8  	"errors"
     9  	"net"
    10  	"net/http"
    11  
    12  	"github.com/SlyMarbo/spdy/common"
    13  	"github.com/SlyMarbo/spdy/spdy2"
    14  	"github.com/SlyMarbo/spdy/spdy3"
    15  )
    16  
    17  // init modifies http.DefaultClient to use a spdy.Transport, enabling
    18  // support for SPDY in functions like http.Get.
    19  func init() {
    20  	http.DefaultClient = NewClient(false)
    21  }
    22  
    23  // NewClientConn is used to create a SPDY connection, using the given
    24  // net.Conn for the underlying connection, and the given Receiver to
    25  // receive server pushes.
    26  func NewClientConn(conn net.Conn, push common.Receiver, version, subversion int) (common.Conn, error) {
    27  	if conn == nil {
    28  		return nil, errors.New("Error: Connection initialised with nil net.conn.")
    29  	}
    30  
    31  	switch version {
    32  	case 3:
    33  		out := spdy3.NewConn(conn, nil, subversion)
    34  		out.PushReceiver = push
    35  		return out, nil
    36  
    37  	case 2:
    38  		out := spdy2.NewConn(conn, nil)
    39  		out.PushReceiver = push
    40  		return out, nil
    41  
    42  	default:
    43  		return nil, errors.New("Error: Unrecognised SPDY version.")
    44  	}
    45  }
    46  
    47  // NewClient creates an http.Client that supports SPDY.
    48  func NewClient(insecureSkipVerify bool) *http.Client {
    49  	return &http.Client{Transport: NewTransport(insecureSkipVerify)}
    50  }