Skip to content

Commit

Permalink
Merge pull request prasmussen#374 from ipopov/master
Browse files Browse the repository at this point in the history
Command line flag to disable HTTP compression.

* git://github.com/prasmussen/gdrive:
  Command line flag to disable HTTP compression.
  • Loading branch information
msfjarvis committed Nov 11, 2018
2 parents 7e8aacb + f9f9178 commit adbfb6b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
17 changes: 9 additions & 8 deletions auth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package auth

import (
"fmt"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"net/http"
Expand All @@ -10,7 +11,7 @@ import (

type authCodeFn func(string) func() string

func NewFileSourceClient(clientId, clientSecret, tokenFile string, authFn authCodeFn) (*http.Client, error) {
func NewFileSourceClient(clientId, clientSecret string, ctx context.Context, tokenFile string, authFn authCodeFn) (*http.Client, error) {
conf := getConfig(clientId, clientSecret)

// Read cached token
Expand All @@ -31,12 +32,12 @@ func NewFileSourceClient(clientId, clientSecret, tokenFile string, authFn authCo
}

return oauth2.NewClient(
oauth2.NoContext,
ctx,
FileSource(tokenFile, token, conf),
), nil
}

func NewRefreshTokenClient(clientId, clientSecret, refreshToken string) *http.Client {
func NewRefreshTokenClient(clientId, clientSecret string, ctx context.Context, refreshToken string) *http.Client {
conf := getConfig(clientId, clientSecret)

token := &oauth2.Token{
Expand All @@ -46,12 +47,12 @@ func NewRefreshTokenClient(clientId, clientSecret, refreshToken string) *http.Cl
}

return oauth2.NewClient(
oauth2.NoContext,
ctx,
conf.TokenSource(oauth2.NoContext, token),
)
}

func NewAccessTokenClient(clientId, clientSecret, accessToken string) *http.Client {
func NewAccessTokenClient(clientId, clientSecret string, ctx context.Context, accessToken string) *http.Client {
conf := getConfig(clientId, clientSecret)

token := &oauth2.Token{
Expand All @@ -60,12 +61,12 @@ func NewAccessTokenClient(clientId, clientSecret, accessToken string) *http.Clie
}

return oauth2.NewClient(
oauth2.NoContext,
ctx,
conf.TokenSource(oauth2.NoContext, token),
)
}

func NewServiceAccountClient(serviceAccountFile string) (*http.Client, error) {
func NewServiceAccountClient(serviceAccountFile string, ctx context.Context) (*http.Client, error) {
content, exists, err := ReadFile(serviceAccountFile)
if(!exists) {
return nil, fmt.Errorf("Service account filename %q not found", serviceAccountFile)
Expand All @@ -79,7 +80,7 @@ func NewServiceAccountClient(serviceAccountFile string) (*http.Client, error) {
if(err != nil) {
return nil, err
}
return conf.Client(oauth2.NoContext), nil
return conf.Client(ctx), nil
}

func getConfig(clientId, clientSecret string) *oauth2.Config {
Expand Down
5 changes: 5 additions & 0 deletions gdrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ func main() {
Patterns: []string{"--service-account"},
Description: "Oauth service account filename, used for server to server communication without user interaction (filename path is relative to config dir)",
},
cli.BoolFlag{
Name: "disable-compression",
Patterns: []string{"--disable-compression"},
Description: "Disable gzip compression in HTTP requests. This might be useful to trade higher bandwidth for reduced CPU.",
OmitValue: true},
}

handlers := []*cli.Handler{
Expand Down
16 changes: 12 additions & 4 deletions handlers_drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"fmt"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"io"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -345,27 +347,33 @@ func getOauthClient(args cli.Arguments) (*http.Client, error) {
ExitF("Access token not needed when refresh token is provided")
}

oauth_context := context.TODO()
if args.Bool("disable-compression") {
oauth_context = context.WithValue(oauth_context, oauth2.HTTPClient,
&http.Client{Transport: &http.Transport{DisableCompression: true}})
}

if args.String("refreshToken") != "" {
return auth.NewRefreshTokenClient(ClientId, ClientSecret, args.String("refreshToken")), nil
return auth.NewRefreshTokenClient(ClientId, ClientSecret, oauth_context, args.String("refreshToken")), nil
}

if args.String("accessToken") != "" {
return auth.NewAccessTokenClient(ClientId, ClientSecret, args.String("accessToken")), nil
return auth.NewAccessTokenClient(ClientId, ClientSecret, oauth_context, args.String("accessToken")), nil
}

configDir := getConfigDir(args)

if args.String("serviceAccount") != "" {
serviceAccountPath := ConfigFilePath(configDir, args.String("serviceAccount"))
serviceAccountClient, err := auth.NewServiceAccountClient(serviceAccountPath)
serviceAccountClient, err := auth.NewServiceAccountClient(serviceAccountPath, oauth_context)
if err != nil {
return nil, err
}
return serviceAccountClient, nil
}

tokenPath := ConfigFilePath(configDir, TokenFilename)
return auth.NewFileSourceClient(ClientId, ClientSecret, tokenPath, authCodePrompt)
return auth.NewFileSourceClient(ClientId, ClientSecret, oauth_context, tokenPath, authCodePrompt)
}

func getConfigDir(args cli.Arguments) string {
Expand Down

0 comments on commit adbfb6b

Please sign in to comment.