<commit_before>// Copyright 2015 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package cmd

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net"
	"net/http"
	"strings"

	"github.com/tsuru/tsuru/exec"
)

var execut exec.Executor

const callbackPage = `<!DOCTYPE html>
<html>
<head>
	<style>
	body {
		text-align: center;
	}
	</style>
</head>
<body>
	%s
</body>
</html>
`

const successMarkup = `
	<script>window.close();</script>
	<h1>Login Successful!</h1>
	<p>You can close this window now.</p>
`

const errorMarkup = `
	<h1>Login Failed!</h1>
	<p>%s</p>
`

func executor() exec.Executor {
	if execut == nil {
		execut = exec.OsExecutor{}
	}
	return execut
}

func port(schemeData map[string]string) string {
	p := schemeData["port"]
	if p != "" {
		return fmt.Sprintf(":%s", p)
	}
	return ":0"
}

func convertToken(code, redirectUrl string) (string, error) {
	var token string
	params := map[string]string{"code": code, "redirectUrl": redirectUrl}
	u, err := GetURL("/auth/login")
	if err != nil {
		return token, fmt.Errorf("Error in GetURL: %s", err.Error())
	}
	var buf bytes.Buffer
	err = json.NewEncoder(&buf).Encode(params)
	if err != nil {
		return token, fmt.Errorf("Error encoding params %#v: %s", params, err.Error())
	}
	resp, err := http.Post(u, "application/json", &buf)
	if err != nil {
		return token, fmt.Errorf("Error during login post: %s", err.Error())
	}
	defer resp.Body.Close()
	result, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return token, fmt.Errorf("Error reading body: %s", err.Error())
	}
	data := make(map[string]interface{})
	err = json.Unmarshal(result, &data)
	if err != nil {
		return token, fmt.Errorf("Error parsing response: %s - %s", result, err.Error())<commit_msg>cmd/oauth: send data as x-www-form-urlencoded<commit_after>// Copyright 2015 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package cmd

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net"
	"net/http"
	"net/url"
	"strings"

	"github.com/tsuru/tsuru/exec"
)

var execut exec.Executor

const callbackPage = `<!DOCTYPE html>
<html>
<head>
	<style>
	body {
		text-align: center;
	}
	</style>
</head>
<body>
	%s
</body>
</html>
`

const successMarkup = `
	<script>window.close();</script>
	<h1>Login Successful!</h1>
	<p>You can close this window now.</p>
`

const errorMarkup = `
	<h1>Login Failed!</h1>
	<p>%s</p>
`

func executor() exec.Executor {
	if execut == nil {
		execut = exec.OsExecutor{}
	}
	return execut
}

func port(schemeData map[string]string) string {
	p := schemeData["port"]
	if p != "" {
		return fmt.Sprintf(":%s", p)
	}
	return ":0"
}

func convertToken(code, redirectUrl string) (string, error) {
	var token string
	v := url.Values{}
	v.Set("code", code)
	v.Set("redirectUrl", redirectUrl)
	u, err := GetURL("/auth/login")
	if err != nil {
		return token, fmt.Errorf("Error in GetURL: %s", err.Error())
	}
	resp, err := http.Post(u, "application/x-www-form-urlencoded", strings.NewReader(v.Encode()))
	if err != nil {
		return token, fmt.Errorf("Error during login post: %s", err.Error())
	}
	defer resp.Body.Close()
	result, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return token, fmt.Errorf("Error reading body: %s", err.Error())
	}
	data := make(map[string]interface{})
	err = json.Unmarshal(result, &data)
	if err != nil {
		return token, fmt.Errorf("Error parsing response: %s - %s", result, err.Error())
{'fraction_non_alphanumeric': 0.1728117084154236, 'fraction_numerical': 0.0050661412890515055, 'mean_word_length': 3.9706293706293705, 'pattern_counts': {'":': 6, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3456114', 'n_tokens_mistral': 1354, 'n_tokens_neox': 1322, 'n_words': 363}