<commit_before>// Package lg provides looking glass methods for selected looking glasses
// Telia Carrier Looking Glass ASN 1299
package lg

import (
	"bufio"
	"errors"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"regexp"
	"sort"
)

// A Telia represents a telia looking glass request
type Telia struct {
	Host  string
	IPv   string
	Node  string
	Nodes []string
}

var teliaDefaultNode = "Los Angeles"

// Set configures host and ip version
func (p *Telia) Set(host, version string) {
	p.Host = host
	p.IPv = version
	if p.Node == "" {
		p.Node = teliaDefaultNode
	}
}

// GetDefaultNode returns telia default node
func (p *Telia) GetDefaultNode() string {
	return teliaDefaultNode
}

// GetNodes returns all Telia nodes (US and International)
func (p *Telia) GetNodes() []string {
	// Memory cache
	if len(p.Nodes) > 1 {
		return p.Nodes
	}
	var nodes []string
	for node := range p.FetchNodes() {
		nodes = append(nodes, node)
	}
	sort.Strings(nodes)
	p.Nodes = nodes
	return nodes
}

// ChangeNode set new requested node
func (p *Telia) ChangeNode(node string) {
	var valid = false
	// Validate
	for _, n := range p.Nodes {
		if node == n {
			valid = true
			break
		}
	}
	if valid {
		p.Node = node
	} else {
		p.Node = "NA"
		println("Invalid node please press tab after node command to show the valid nodes")
	}
}

// Ping tries to connect Telia's ping looking glass through HTTP
// Returns the result
func (p *Telia) Ping() (string, error) {
	// Basic validate
	if p.Node == "NA" || len(p.Host) < 5 {
		print("Invalid node or host/ip address")
		return "", errors.New("error")
	}
	resp, err := http.PostForm("http://looking-glass.telia.net/",
		url.Values{"query": {"ping"}, "protocol": {p.IPv}, "addr": {p.Host}, "router": {p.Node}})
	if err != nil {
		println(err)
		return "", err
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", err
	}
	r, _ := regexp.Compile(`<CODE>(?s)(.*?)</CODE>`)
	b := r.FindStringSubmatch(string(body))
	if len(b) > 0 {
		return b[1], nil
	}
	return "", errors.New("error")
}

// Trace gets traceroute information from Telia
func (p *Telia) Trace() chan string {
	c := make(chan string)
	resp, err := http.PostForm("http://looking-glass.telia.net/",
		url.Values{"query": {"trace"}, "protocol": {p.IPv}, "addr": {p.Host}, "router": {p.Node}})
	if err != nil {
		println(err)
	}
	go func() {
		defer resp.Body.Close()
		scanner := bufio.NewScanner(resp.Body)
		for scanner.Scan() {
			l := scanner.Text()
			m, _ := regexp.MatchString(`^(traceroute|\s*\d{1,2})`, l)
			if m {
				l = replaceASNTrace(l)
				c <- l
			}
		}
		close(c)
	}()
	return c
}

// BGP gets bgp informtion from Telia
func (p *Telia) BGP() chan string {
	c := make(chan string)
	resp, err := http.PostForm("http://looking-glass.telia.net/",
		url.Values{"query": {"bgp"}, "protocol": {p.IPv}, "addr": {p.Host}, "router": {p.Node}})
	if err != nil {
		println(err)
	}
	go func() {
		var (
			parse = false
			last  string
		)
		defer resp.Body.Close()
		scanner := bufio.NewScanner(resp.Body)
		for scanner.Scan() {
			l := scanner.Text()
			l = sanitize(l)
			if m, _ := regexp.MatchString("Telia Carrier", l); !parse && m {
				parse = true
				continue
			}
			if !parse || (l == last) {
				continue
			}
			c <- l
			last = l
		}
		close(c)
	}()
	return c
}

//FetchNodes returns all available nodes through HTTP
func (p *Telia) FetchNodes() map[string]string {
	var nodes = make(map[string]string, 100)
	resp, err := http.Get("http://looking-glass.telia.net/")
	if err != nil {
		println("error: telia looking glass unreachable (1) ")
		return map[string]string{}
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		println("error: telia looking glass unreachable (2)" + err.Error())
		return map[string]string{}
	}
	r, _ := regexp.Compile(`(?i)<option value="(?s)([\w|\s|)(._-]+)"> (?s)([\w|\s|)(._-]+)`)
	b := r.FindAllStringSubmatch(string(body), -1)
	for _, v := range b {
		nodes[v[1]] = v[2]
	}
	return nodes
}

//[GOOGLE (ARIN)" HREF="http://www.arin.net/cgi-bin/whois.pl?queryinput=15169" TARGET=_lookup>15169]  1.261 ms 72.14.236.69 (72.14.236.69) [AS  15169]
// replaceASNTrace
func replaceASNTrace(l string) string {
	m, _ := regexp.MatchString(`\[AS\s+`, l)
	if !m {
		return l
	}
	r := regexp.MustCompile(`(?i)\[AS\s+<A\s+title="([a-z|\d|\s|\(\)_,-]+)"\s+HREF="[a-z|\/|:.-]+\?\w+=\d+"\s+\w+=_lookup>(\d+)</A>]`)
	asn := r.FindStringSubmatch(l)
	if len(asn) == 3 {
		l = r.ReplaceAllString(l, fmt.Sprintf("[%s (%s)]", asn[1], asn[2]))
	}
	return l
}
<commit_msg>handle none 200 http status code<commit_after>// Package lg provides looking glass methods for selected looking glasses
// Telia Carrier Looking Glass ASN 1299
package lg

import (
	"bufio"
	"errors"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"regexp"
	"sort"
)

// A Telia represents a telia looking glass request
type Telia struct {
	Host  string
	IPv   string
	Node  string
	Nodes []string
}

var teliaDefaultNode = "Los Angeles"

// Set configures host and ip version
func (p *Telia) Set(host, version string) {
	p.Host = host
	p.IPv = version
	if p.Node == "" {
		p.Node = teliaDefaultNode
	}
}

// GetDefaultNode returns telia default node
func (p *Telia) GetDefaultNode() string {
	return teliaDefaultNode
}

// GetNodes returns all Telia nodes (US and International)
func (p *Telia) GetNodes() []string {
	// Memory cache
	if len(p.Nodes) > 1 {
		return p.Nodes
	}
	var nodes []string
	for node := range p.FetchNodes() {
		nodes = append(nodes, node)
	}
	sort.Strings(nodes)
	p.Nodes = nodes
	return nodes
}

// ChangeNode set new requested node
func (p *Telia) ChangeNode(node string) {
	var valid = false
	// Validate
	for _, n := range p.Nodes {
		if node == n {
			valid = true
			break
		}
	}
	if valid {
		p.Node = node
	} else {
		p.Node = "NA"
		println("Invalid node please press tab after node command to show the valid nodes")
	}
}

// Ping tries to connect Telia's ping looking glass through HTTP
// Returns the result
func (p *Telia) Ping() (string, error) {
	// Basic validate
	if p.Node == "NA" || len(p.Host) < 5 {
		print("Invalid node or host/ip address")
		return "", errors.New("error")
	}
	resp, err := http.PostForm("http://looking-glass.telia.net/",
		url.Values{"query": {"ping"}, "protocol": {p.IPv}, "addr": {p.Host}, "router": {p.Node}})
	if err != nil {
		return "", err
	}
	if resp.StatusCode != 200 {
		return "", errors.New("error: level3 looking glass is not available")
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", err
	}
	r, _ := regexp.Compile(`<CODE>(?s)(.*?)</CODE>`)
	b := r.FindStringSubmatch(string(body))
	if len(b) > 0 {
		return b[1], nil
	}
	return "", errors.New("error")
}

// Trace gets traceroute information from Telia
func (p *Telia) Trace() chan string {
	c := make(chan string)
	resp, err := http.PostForm("http://looking-glass.telia.net/",
		url.Values{"query": {"trace"}, "protocol": {p.IPv}, "addr": {p.Host}, "router": {p.Node}})
	if err != nil {
		println(err)
	}
	go func() {
		defer resp.Body.Close()
		scanner := bufio.NewScanner(resp.Body)
		for scanner.Scan() {
			l := scanner.Text()
			m, _ := regexp.MatchString(`^(traceroute|\s*\d{1,2})`, l)
			if m {
				l = replaceASNTrace(l)
				c <- l
			}
		}
		close(c)
	}()
	return c
}

// BGP gets bgp informtion from Telia
func (p *Telia) BGP() chan string {
	c := make(chan string)
	resp, err := http.PostForm("http://looking-glass.telia.net/",
		url.Values{"query": {"bgp"}, "protocol": {p.IPv}, "addr": {p.Host}, "router": {p.Node}})
	if err != nil {
		println(err)
	}
	go func() {
		var (
			parse = false
			last  string
		)
		defer resp.Body.Close()
		scanner := bufio.NewScanner(resp.Body)
		for scanner.Scan() {
			l := scanner.Text()
			l = sanitize(l)
			if m, _ := regexp.MatchString("Telia Carrier", l); !parse && m {
				parse = true
				continue
			}
			if !parse || (l == last) {
				continue
			}
			c <- l
			last = l
		}
		close(c)
	}()
	return c
}

//FetchNodes returns all available nodes through HTTP
func (p *Telia) FetchNodes() map[string]string {
	var nodes = make(map[string]string, 100)
	resp, err := http.Get("http://looking-glass.telia.net/")
	if err != nil {
		println("error: telia looking glass unreachable (1) ")
		return map[string]string{}
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		println("error: telia looking glass unreachable (2)" + err.Error())
		return map[string]string{}
	}
	r, _ := regexp.Compile(`(?i)<option value="(?s)([\w|\s|)(._-]+)"> (?s)([\w|\s|)(._-]+)`)
	b := r.FindAllStringSubmatch(string(body), -1)
	for _, v := range b {
		nodes[v[1]] = v[2]
	}
	return nodes
}

//[GOOGLE (ARIN)" HREF="http://www.arin.net/cgi-bin/whois.pl?queryinput=15169" TARGET=_lookup>15169]  1.261 ms 72.14.236.69 (72.14.236.69) [AS  15169]
// replaceASNTrace
func replaceASNTrace(l string) string {
	m, _ := regexp.MatchString(`\[AS\s+`, l)
	if !m {
		return l
	}
	r := regexp.MustCompile(`(?i)\[AS\s+<A\s+title="([a-z|\d|\s|\(\)_,-]+)"\s+HREF="[a-z|\/|:.-]+\?\w+=\d+"\s+\w+=_lookup>(\d+)</A>]`)
	asn := r.FindStringSubmatch(l)
	if len(asn) == 3 {
		l = r.ReplaceAllString(l, fmt.Sprintf("[%s (%s)]", asn[1], asn[2]))
	}
	return l
}
<|endoftext|>


<commit_before>package dnsimple

import (
	"fmt"
	"os"
	"strconv"
	"strings"

	"github.com/Sirupsen/logrus"
	"github.com/dnsimple/dnsimple-go/dnsimple"
	"github.com/juju/ratelimit"
	"github.com/rancher/external-dns/providers"
	"github.com/rancher/external-dns/utils"
	api "github.com/weppos/go-dnsimple/dnsimple"
)

type DNSimpleProvider struct {
	client    *api.Client
	client2   *dnsimple.Client
	accountID string
	root      string
	limiter   *ratelimit.Bucket
}

func init() {
	providers.RegisterProvider("dnsimple", &DNSimpleProvider{})
}

func (d *DNSimpleProvider) Init(rootDomainName string) error {
	var email, apiToken, oauthToken string

	if email = os.Getenv("DNSIMPLE_EMAIL"); len(email) == 0 {
		return fmt.Errorf("DNSIMPLE_EMAIL is not set")
	}

	if apiToken = os.Getenv("DNSIMPLE_TOKEN"); len(apiToken) == 0 {
		return fmt.Errorf("DNSIMPLE_TOKEN is not set")
	}

	if oauthToken = os.Getenv("DNSIMPLE_TOKEN"); len(oauthToken) == 0 {
		return fmt.Errorf("DNSIMPLE_TOKEN is not set")
	}

	d.root = utils.UnFqdn(rootDomainName)
	d.client = api.NewClient(apiToken, email)
	d.client2 = dnsimple.NewClient(dnsimple.NewOauthTokenCredentials(oauthToken))
	d.limiter = ratelimit.NewBucketWithRate(1.5, 5)

	whoamiResponse, err := d.client2.Identity.Whoami()
	if err != nil {
		return fmt.Errorf("DNSimple Authentication failed: %v", err)
	}
	if whoamiResponse.Data.Account == nil {
		return fmt.Errorf("DNSimple User tokens are not supported, use an Account token")
	}
	d.accountID = strconv.Itoa(whoamiResponse.Data.Account.ID)

	_, err = d.client2.Zones.GetZone(d.accountID, d.root)
	if err != nil {
		return fmt.Errorf("Failed to get zone for '%s': %v", d.root)
	}

	logrus.Infof("Configured %s with zone '%s'", d.GetName(), d.root)
	return nil
}

func (*DNSimpleProvider) GetName() string {
	return "DNSimple"
}

func (d *DNSimpleProvider) HealthCheck() error {
	d.limiter.Wait(1)
	_, err := d.client2.Identity.Whoami()
	return err
}

func (d *DNSimpleProvider) parseName(record utils.DnsRecord) string {
	name := strings.TrimSuffix(record.Fqdn, fmt.Sprintf(".%s.", d.root))
	return name
}

func (d *DNSimpleProvider) AddRecord(record utils.DnsRecord) error {
	name := d.parseName(record)
	for _, rec := range record.Records {
		recordInput := dnsimple.ZoneRecord{
			Name:    name,
			TTL:     record.TTL,
			Type:    record.Type,
			Content: rec,
		}
		d.limiter.Wait(1)
		_, err := d.client2.Zones.CreateRecord(d.accountID, d.root, recordInput)
		if err != nil {
			return fmt.Errorf("DNSimple API call has failed: %v", err)
		}
	}

	return nil
}

func (d *DNSimpleProvider) findRecords(record utils.DnsRecord) ([]api.Record, error) {
	var records []api.Record

	d.limiter.Wait(1)
	resp, _, err := d.client.Domains.ListRecords(d.root, "", "")
	if err != nil {
		return records, fmt.Errorf("DNSimple API call has failed: %v", err)
	}

	name := d.parseName(record)
	for _, rec := range resp {
		if rec.Name == name && rec.Type == record.Type {
			records = append(records, rec)
		}
	}

	return records, nil
}

func (d *DNSimpleProvider) UpdateRecord(record utils.DnsRecord) error {
	err := d.RemoveRecord(record)
	if err != nil {
		return err
	}

	return d.AddRecord(record)
}

func (d *DNSimpleProvider) RemoveRecord(record utils.DnsRecord) error {
	records, err := d.findRecords(record)
	if err != nil {
		return err
	}

	for _, rec := range records {
		d.limiter.Wait(1)
		_, err := d.client2.Zones.DeleteRecord(d.accountID, d.root, rec.Id)
		if err != nil {
			return fmt.Errorf("DNSimple API call has failed: %v", err)
		}
	}

	return nil
}

func (d *DNSimpleProvider) GetRecords() ([]utils.DnsRecord, error) {
	var records []utils.DnsRecord

	d.limiter.Wait(1)
	recordResp, _, err := d.client.Domains.ListRecords(d.root, "", "")
	if err != nil {
		return records, fmt.Errorf("DNSimple API call has failed: %v", err)
	}

	recordMap := map[string]map[string][]string{}
	recordTTLs := map[string]map[string]int{}

	for _, rec := range recordResp {
		var fqdn string
		if rec.Name == "" {
			fqdn = d.root + "."
		} else {
			fqdn = fmt.Sprintf("%s.%s.", rec.Name, d.root)
		}

		recordTTLs[fqdn] = map[string]int{}
		recordTTLs[fqdn][rec.Type] = rec.TTL
		recordSet, exists := recordMap[fqdn]
		if exists {
			recordSlice, sliceExists := recordSet[rec.Type]
			if sliceExists {
				recordSlice = append(recordSlice, rec.Content)
				recordSet[rec.Type] = recordSlice
			} else {
				recordSet[rec.Type] = []string{rec.Content}
			}
		} else {
			recordMap[fqdn] = map[string][]string{}
			recordMap[fqdn][rec.Type] = []string{rec.Content}
		}
	}

	for fqdn, recordSet := range recordMap {
		for recordType, recordSlice := range recordSet {
			ttl := recordTTLs[fqdn][recordType]
			record := utils.DnsRecord{Fqdn: fqdn, Records: recordSlice, Type: recordType, TTL: ttl}
			records = append(records, record)
		}
	}
	return records, nil
}
<commit_msg>Revert "Update RemoveRecord() to use API v2"<commit_after>package dnsimple

import (
	"fmt"
	"os"
	"strconv"
	"strings"

	"github.com/Sirupsen/logrus"
	"github.com/dnsimple/dnsimple-go/dnsimple"
	"github.com/juju/ratelimit"
	"github.com/rancher/external-dns/providers"
	"github.com/rancher/external-dns/utils"
	api "github.com/weppos/go-dnsimple/dnsimple"
)

type DNSimpleProvider struct {
	client    *api.Client
	client2   *dnsimple.Client
	accountID string
	root      string
	limiter   *ratelimit.Bucket
}

func init() {
	providers.RegisterProvider("dnsimple", &DNSimpleProvider{})
}

func (d *DNSimpleProvider) Init(rootDomainName string) error {
	var email, apiToken, oauthToken string

	if email = os.Getenv("DNSIMPLE_EMAIL"); len(email) == 0 {
		return fmt.Errorf("DNSIMPLE_EMAIL is not set")
	}

	if apiToken = os.Getenv("DNSIMPLE_TOKEN"); len(apiToken) == 0 {
		return fmt.Errorf("DNSIMPLE_TOKEN is not set")
	}

	if oauthToken = os.Getenv("DNSIMPLE_TOKEN"); len(oauthToken) == 0 {
		return fmt.Errorf("DNSIMPLE_TOKEN is not set")
	}

	d.root = utils.UnFqdn(rootDomainName)
	d.client = api.NewClient(apiToken, email)
	d.client2 = dnsimple.NewClient(dnsimple.NewOauthTokenCredentials(oauthToken))
	d.limiter = ratelimit.NewBucketWithRate(1.5, 5)

	whoamiResponse, err := d.client2.Identity.Whoami()
	if err != nil {
		return fmt.Errorf("DNSimple Authentication failed: %v", err)
	}
	if whoamiResponse.Data.Account == nil {
		return fmt.Errorf("DNSimple User tokens are not supported, use an Account token")
	}
	d.accountID = strconv.Itoa(whoamiResponse.Data.Account.ID)

	_, err = d.client2.Zones.GetZone(d.accountID, d.root)
	if err != nil {
		return fmt.Errorf("Failed to get zone for '%s': %v", d.root)
	}

	logrus.Infof("Configured %s with zone '%s'", d.GetName(), d.root)
	return nil
}

func (*DNSimpleProvider) GetName() string {
	return "DNSimple"
}

func (d *DNSimpleProvider) HealthCheck() error {
	d.limiter.Wait(1)
	_, err := d.client2.Identity.Whoami()
	return err
}

func (d *DNSimpleProvider) parseName(record utils.DnsRecord) string {
	name := strings.TrimSuffix(record.Fqdn, fmt.Sprintf(".%s.", d.root))
	return name
}

func (d *DNSimpleProvider) AddRecord(record utils.DnsRecord) error {
	name := d.parseName(record)
	for _, rec := range record.Records {
		recordInput := dnsimple.ZoneRecord{
			Name:    name,
			TTL:     record.TTL,
			Type:    record.Type,
			Content: rec,
		}
		d.limiter.Wait(1)
		_, err := d.client2.Zones.CreateRecord(d.accountID, d.root, recordInput)
		if err != nil {
			return fmt.Errorf("DNSimple API call has failed: %v", err)
		}
	}

	return nil
}

func (d *DNSimpleProvider) findRecords(record utils.DnsRecord) ([]api.Record, error) {
	var records []api.Record

	d.limiter.Wait(1)
	resp, _, err := d.client.Domains.ListRecords(d.root, "", "")
	if err != nil {
		return records, fmt.Errorf("DNSimple API call has failed: %v", err)
	}

	name := d.parseName(record)
	for _, rec := range resp {
		if rec.Name == name && rec.Type == record.Type {
			records = append(records, rec)
		}
	}

	return records, nil
}

func (d *DNSimpleProvider) UpdateRecord(record utils.DnsRecord) error {
	err := d.RemoveRecord(record)
	if err != nil {
		return err
	}

	return d.AddRecord(record)
}

func (d *DNSimpleProvider) RemoveRecord(record utils.DnsRecord) error {
	records, err := d.findRecords(record)
	if err != nil {
		return err
	}

	for _, rec := range records {
		d.limiter.Wait(1)
		_, err := d.client.Domains.DeleteRecord(d.root, rec.Id)
		if err != nil {
			return fmt.Errorf("DNSimple API call has failed: %v", err)
		}
	}

	return nil
}

func (d *DNSimpleProvider) GetRecords() ([]utils.DnsRecord, error) {
	var records []utils.DnsRecord

	d.limiter.Wait(1)
	recordResp, _, err := d.client.Domains.ListRecords(d.root, "", "")
	if err != nil {
		return records, fmt.Errorf("DNSimple API call has failed: %v", err)
	}

	recordMap := map[string]map[string][]string{}
	recordTTLs := map[string]map[string]int{}

	for _, rec := range recordResp {
		var fqdn string
		if rec.Name == "" {
			fqdn = d.root + "."
		} else {
			fqdn = fmt.Sprintf("%s.%s.", rec.Name, d.root)
		}

		recordTTLs[fqdn] = map[string]int{}
		recordTTLs[fqdn][rec.Type] = rec.TTL
		recordSet, exists := recordMap[fqdn]
		if exists {
			recordSlice, sliceExists := recordSet[rec.Type]
			if sliceExists {
				recordSlice = append(recordSlice, rec.Content)
				recordSet[rec.Type] = recordSlice
			} else {
				recordSet[rec.Type] = []string{rec.Content}
			}
		} else {
			recordMap[fqdn] = map[string][]string{}
			recordMap[fqdn][rec.Type] = []string{rec.Content}
		}
	}

	for fqdn, recordSet := range recordMap {
		for recordType, recordSlice := range recordSet {
			ttl := recordTTLs[fqdn][recordType]
			record := utils.DnsRecord{Fqdn: fqdn, Records: recordSlice, Type: recordType, TTL: ttl}
			records = append(records, record)
		}
	}
	return records, nil
}
<|endoftext|>


<commit_before>// Package linkedin implements the OAuth2 protocol for authenticating users through Linkedin.
package linkedin

import (
	"encoding/json"
	"errors"
	"github.com/markbates/goth"
	"golang.org/x/oauth2"
	"io"
	"net/http"
	"net/url"
)

//more details about linkedin fields: https://developer.linkedin.com/documents/profile-fields

const (
	authURL  string = "https://www.linkedin.com/uas/oauth2/authorization"
	tokenURL string = "https://www.linkedin.com/uas/oauth2/accessToken"

	//userEndpoint requires scopes "r_basicprofile", "r_emailaddress"
	userEndpoint string = "//api.linkedin.com/v1/people/~:(id,first-name,last-name,headline,location:(name),picture-url,email-address)"
)

// New creates a new linkedin provider, and sets up important connection details.
// You should always call `linkedin.New` to get a new Provider. Never try to create
// one manually.
func New(clientKey, secret, callbackURL string, scopes ...string) *Provider {
	p := &Provider{
		ClientKey:   clientKey,
		Secret:      secret,
		CallbackURL: callbackURL,
	}
	p.config = newConfig(p, scopes)
	return p
}

// Provider is the implementation of `goth.Provider` for accessing Linkedin.
type Provider struct {
	ClientKey   string
	Secret      string
	CallbackURL string
	config      *oauth2.Config
}

// Name is the name used to retrieve this provider later.
func (p *Provider) Name() string {
	return "linkedin"
}

// Debug is a no-op for the linkedin package.
func (p *Provider) Debug(debug bool) {}

// BeginAuth asks Linkedin for an authentication end-point.
func (p *Provider) BeginAuth(state string) (goth.Session, error) {
	url := p.config.AuthCodeURL(state)
	session := &Session{
		AuthURL: url,
	}
	return session, nil
}

// FetchUser will go to Linkedin and access basic information about the user.
func (p *Provider) FetchUser(session goth.Session) (goth.User, error) {
	s := session.(*Session)
	user := goth.User{
		AccessToken: s.AccessToken,
		Provider:    p.Name(),
		ExpiresAt:   s.ExpiresAt,
	}

	req, err := http.NewRequest("GET", "", nil)
	if err != nil {
		return user, err
	}

	//add url as opaque to avoid escaping of "("
	req.URL = &url.URL{
		Scheme: "https",
		Host:   "api.linkedin.com",
		Opaque: userEndpoint,
	}

	req.Header.Set("Authorization", "Bearer "+s.AccessToken)
	req.Header.Add("x-li-format", "json") //request json response
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		if resp != nil {
			resp.Body.Close()
		}
		return user, err
	}
	defer resp.Body.Close()
	//err = userFromReader(io.TeeReader(resp.Body, os.Stdout), &user)
	err = userFromReader(resp.Body, &user)
	return user, err
}

func userFromReader(reader io.Reader, user *goth.User) error {

	u := struct {
		ID         string `json:"id"`
		Email      string `json:"emailAddress"`
		FirstName  string `json:"firstName"`
		LastName   string `json:"lastName"`
		Headline   string `json:"headline"`
		PictureURL string `json:"pictureUrl"`
		Location   struct {
			Name string `json:"name"`
		} `json:"location"`
	}{}

	err := json.NewDecoder(reader).Decode(&u)
	if err != nil {
		return err
	}

	user.Name = u.FirstName + " " + u.LastName
	user.NickName = u.FirstName
	user.Email = u.Email
	user.Description = u.Headline
	user.AvatarURL = u.PictureURL
	user.UserID = u.ID
	user.Location = u.Location.Name

	return err
}

func newConfig(provider *Provider, scopes []string) *oauth2.Config {
	c := &oauth2.Config{
		ClientID:     provider.ClientKey,
		ClientSecret: provider.Secret,
		RedirectURL:  provider.CallbackURL,
		Endpoint: oauth2.Endpoint{
			AuthURL:  authURL,
			TokenURL: tokenURL,
		},
		Scopes: []string{},
	}

	for _, scope := range scopes {
		c.Scopes = append(c.Scopes, scope)
	}

	return c
}

//RefreshToken refresh token is not provided by linkedin
func (p *Provider) RefreshToken(refreshToken string) (*oauth2.Token, error) {
	return nil, errors.New("Refresh token is not provided by linkedin")
}

//RefreshTokenAvailable refresh token is not provided by linkedin
func (p *Provider) RefreshTokenAvailable() bool {
	return false
}
<commit_msg>Update LinkedIn OAuth Endpoints to latest correct version<commit_after>// Package linkedin implements the OAuth2 protocol for authenticating users through Linkedin.
package linkedin

import (
	"encoding/json"
	"errors"
	"github.com/markbates/goth"
	"golang.org/x/oauth2"
	"io"
	"net/http"
	"net/url"
)

//more details about linkedin fields: https://developer.linkedin.com/documents/profile-fields

const (
	authURL  string = "https://www.linkedin.com/oauth/v2/authorization"
	tokenURL string = "https://www.linkedin.com/oauth/v2/accessToken"

	//userEndpoint requires scopes "r_basicprofile", "r_emailaddress"
	userEndpoint string = "//api.linkedin.com/v1/people/~:(id,first-name,last-name,headline,location:(name),picture-url,email-address)"
)

// New creates a new linkedin provider, and sets up important connection details.
// You should always call `linkedin.New` to get a new Provider. Never try to create
// one manually.
func New(clientKey, secret, callbackURL string, scopes ...string) *Provider {
	p := &Provider{
		ClientKey:   clientKey,
		Secret:      secret,
		CallbackURL: callbackURL,
	}
	p.config = newConfig(p, scopes)
	return p
}

// Provider is the implementation of `goth.Provider` for accessing Linkedin.
type Provider struct {
	ClientKey   string
	Secret      string
	CallbackURL string
	config      *oauth2.Config
}

// Name is the name used to retrieve this provider later.
func (p *Provider) Name() string {
	return "linkedin"
}

// Debug is a no-op for the linkedin package.
func (p *Provider) Debug(debug bool) {}

// BeginAuth asks Linkedin for an authentication end-point.
func (p *Provider) BeginAuth(state string) (goth.Session, error) {
	url := p.config.AuthCodeURL(state)
	session := &Session{
		AuthURL: url,
	}
	return session, nil
}

// FetchUser will go to Linkedin and access basic information about the user.
func (p *Provider) FetchUser(session goth.Session) (goth.User, error) {
	s := session.(*Session)
	user := goth.User{
		AccessToken: s.AccessToken,
		Provider:    p.Name(),
		ExpiresAt:   s.ExpiresAt,
	}

	req, err := http.NewRequest("GET", "", nil)
	if err != nil {
		return user, err
	}

	//add url as opaque to avoid escaping of "("
	req.URL = &url.URL{
		Scheme: "https",
		Host:   "api.linkedin.com",
		Opaque: userEndpoint,
	}

	req.Header.Set("Authorization", "Bearer "+s.AccessToken)
	req.Header.Add("x-li-format", "json") //request json response
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		if resp != nil {
			resp.Body.Close()
		}
		return user, err
	}
	defer resp.Body.Close()
	//err = userFromReader(io.TeeReader(resp.Body, os.Stdout), &user)
	err = userFromReader(resp.Body, &user)
	return user, err
}

func userFromReader(reader io.Reader, user *goth.User) error {

	u := struct {
		ID         string `json:"id"`
		Email      string `json:"emailAddress"`
		FirstName  string `json:"firstName"`
		LastName   string `json:"lastName"`
		Headline   string `json:"headline"`
		PictureURL string `json:"pictureUrl"`
		Location   struct {
			Name string `json:"name"`
		} `json:"location"`
	}{}

	err := json.NewDecoder(reader).Decode(&u)
	if err != nil {
		return err
	}

	user.Name = u.FirstName + " " + u.LastName
	user.NickName = u.FirstName
	user.Email = u.Email
	user.Description = u.Headline
	user.AvatarURL = u.PictureURL
	user.UserID = u.ID
	user.Location = u.Location.Name

	return err
}

func newConfig(provider *Provider, scopes []string) *oauth2.Config {
	c := &oauth2.Config{
		ClientID:     provider.ClientKey,
		ClientSecret: provider.Secret,
		RedirectURL:  provider.CallbackURL,
		Endpoint: oauth2.Endpoint{
			AuthURL:  authURL,
			TokenURL: tokenURL,
		},
		Scopes: []string{},
	}

	for _, scope := range scopes {
		c.Scopes = append(c.Scopes, scope)
	}

	return c
}

//RefreshToken refresh token is not provided by linkedin
func (p *Provider) RefreshToken(refreshToken string) (*oauth2.Token, error) {
	return nil, errors.New("Refresh token is not provided by linkedin")
}

//RefreshTokenAvailable refresh token is not provided by linkedin
func (p *Provider) RefreshTokenAvailable() bool {
	return false
}
<|endoftext|>


<commit_before>package boardgame

//StorageManager is an interface that anything can implement to handle the
//persistence of Games and States.
type StorageManager interface {
	//State returns the StateWrapper for the game at the given version, or
	//nil.
	State(game *Game, version int) *StateWrapper
	//SaveState puts the given stateWrapper (at the specified version and
	//schema) into storage.
	SaveState(game *Game, state *StateWrapper) error
}
<commit_msg>Implementation for inMemoryStorageManager.State(). Part of #81.<commit_after>package boardgame

import (
	"errors"
)

//StorageManager is an interface that anything can implement to handle the
//persistence of Games and States.
type StorageManager interface {
	//State returns the StateWrapper for the game at the given version, or
	//nil.
	State(game *Game, version int) *StateWrapper
	//SaveState puts the given stateWrapper (at the specified version and
	//schema) into storage.
	SaveState(game *Game, state *StateWrapper) error
}

type memoryStateRecord struct {
	Schema          int
	Version         int
	SerializedState []byte
}

type inMemoryStorageManager struct {
	states map[string]map[int]*memoryStateRecord
}

func NewInMemoryStorageManager() StorageManager {
	//InMemoryStorageManager is an extremely simple StorageManager that just keeps
	//track of the objects in memory.
	return &inMemoryStorageManager{
		states: make(map[string]map[int]*memoryStateRecord),
	}
}

func (i *inMemoryStorageManager) State(game *Game, version int) *StateWrapper {
	if game == nil {
		return nil
	}

	if version < 0 || version > game.StateWrapper.Version {
		return nil
	}

	versionMap, ok := i.states[game.Id()]

	if !ok {
		return nil
	}

	record, ok := versionMap[version]

	if !ok {
		return nil
	}

	state, err := game.Delegate.StateFromBlob(record.SerializedState, record.Schema)

	if err != nil {
		return nil
	}

	return &StateWrapper{
		Version: record.Version,
		Schema:  record.Schema,
		State:   state,
	}
}

func (i *inMemoryStorageManager) SaveState(game *Game, state *StateWrapper) error {
	//TODO: do something
	return errors.New("That method is not yet implemented")
}
<|endoftext|>


<commit_before>package phputil

import (
	"crypto/md5"
	"crypto/sha1"
	"crypto/sha256"
	"crypto/sha512"
	"encoding/hex"
	"fmt"
	"hash/crc32"
	"html"
	"io"
	"log"
	"os"
	"strings"
	"time"

	"golang.org/x/crypto/blowfish"
)

func AddcSlashes(s string, c string) string {
	r := strings.Split(c, "..")
	var res string
	min := []rune(r[0])
	max := []rune(r[len(r)-1])
	for _, v := range s {
		if min[0] <= v && v <= max[0] {
			res += "\\" + string(v)
		} else {
			res += string(v)
		}
	}

	return res
}

func AddSlashes(s string) string {
	r := strings.NewReplacer("'", "\\'", "\"", "\\\"", "\\", "\\\\")
	return r.Replace(s)
}

func Bin2Hex(s string) string {
	b := []byte(s[:])
	return hex.EncodeToString(b)
}

func Chop(s ...string) string {
	return trim(s, 1)
}

func Chr(i int) string {
	for i < 0 {
		i += 256
	}
	i %= 256
	return string(rune(i))
}

func ChunkSplit(s string, l int, sep string) string {
	if len(s) < l {
		return s
	}

	res := s[:l] + sep
	tail := s[l:]

	for len(tail) > l {
		res += tail[:l] + sep
		tail = tail[l:]
	}
	res += tail

	return res
}

// TODO
func ConvertCyrString(s string, from string, to string) string {
	return ""
}

// TODO
func ConvertUudecode(s string) string {
	return ""
}

// TODO
func ConvertUuencode(s string) string {
	return ""
}

func CountChars(s string, i int) map[int]int {

	r := countChars(s)

	switch i {
	case 0:
		return r
	case 1:
		for n := 0; n < 255; n++ {
			if r[n] == 0 {
				delete(r, n)
			}
		}
		return r
	case 2:
		for n := 0; n < 255; n++ {
			if r[n] > 0 {
				delete(r, n)
			}
		}
		return r
	}

	return r
}

func CountChars34(s string, i int) string {
	r := countChars(s)
	var res string
	switch i {
	case 3:
		for n := 0; n < 255; n++ {
			if r[n] > 0 {
				res += string(n)
			}
		}
		return res
	case 4:
		for n := 0; n < 255; n++ {
			if r[n] == 0 {
				res += string(n)
			}
		}
		return res
	}
	return res
}

func countChars(s string) map[int]int {
	r := make(map[int]int)
	for i := 0; i < 255; i++ {
		r[i] = 0
	}

	for _, v := range s {
		r[int(v)]++
	}
	return r
}

func Crc32(s string) uint32 {
	return crc32.ChecksumIEEE([]byte(s))
}

// TODO
func Crypt(s string, salt string) string {
	var res string

	switch len(salt) {
	/**
	case 2:
		// CRYPT_STD_DES
		c, _ := des.NewCipher([]byte(salt))
		encrypted := make([]byte, des.BlockSize)
		// DES で暗号化をおこなう
		c.Encrypt(encrypted, []byte(s))
		return string(encrypted)
	case 9:
	// CRYPT_EXT_DES
	**/
	case 12:
		// CRYPT_MD5
		h := md5.New()
		h.Write([]byte(s))
		return hex.EncodeToString(h.Sum(nil))
	case 22:
		// TODO: PADDING salt. CRYPT_BLOWFISH
		cipher, _ := blowfish.NewSaltedCipher([]byte("key"), []byte(salt))
		if len(s)%blowfish.BlockSize != 0 {
			os.Exit(1)
		}
		var encrypted []byte
		cipher.Encrypt(encrypted, []byte(s))
		return string(encrypted)
	case 16:
		// TODO: if condition. CRYPT_SHA256
		if true {
			c := sha256.Sum256([]byte(s))
			return hex.EncodeToString(c[:])
		} else {
			// CRYPT_SHA51
			c := sha512.Sum512([]byte(s))
			return hex.EncodeToString(c[:])
		}

	}
	return res
}

func echo(s string) {
	fmt.Print(s)
}

func Explode(d string, s string, l int) []string {
	return strings.SplitN(s, d, l)
}

func Fprintf(w io.Writer, f string, a ...interface{}) int {
	n, _ := fmt.Fprintf(w, f, a[:]...)
	return n
}

// TODO
func GetHtmlTranslationTable(t int, f int, e string) map[string]string {
	var res map[string]string
	return res
}

// TODO
func Hebrev(s string, m int) string {
	return ""
}

// TODO
func Hebrevc(s string, m int) string {
	return ""
}

func Hex2Bin(s string) string {
	b, _ := hex.DecodeString(s)
	return string(b)
}

func HtmlEntityDecode(s string) string {
	return html.UnescapeString(s)
}

func HtmlEntities(s string) string {
	return html.EscapeString(s)
}

// TODO Fix. It's not strict.
func HtmlspecialcharsDecode(s string) string {
	return html.UnescapeString(s)
}

// TODO Fix. It's not strict.
func Htmlspecialchars(s string) string {
	return html.EscapeString(s)
}

func Implode(d string, s []string) string {
	return strings.Join(s, d)
}

func Join(d string, s []string) string {
	return Implode(d, s)
}

func Levenshtein(s string, t string) int {
	d := make([][]int, len(s)+1)
	for i := range d {
		d[i] = make([]int, len(t)+1)
	}
	for i := range d {
		d[i][0] = i
	}
	for j := range d[0] {
		d[0][j] = j
	}
	for j := 1; j <= len(t); j++ {
		for i := 1; i <= len(s); i++ {
			if s[i-1] == t[j-1] {
				d[i][j] = d[i-1][j-1]
			} else {
				min := d[i-1][j]
				if d[i][j-1] < min {
					min = d[i][j-1]
				}
				if d[i-1][j-1] < min {
					min = d[i-1][j-1]
				}
				d[i][j] = min + 1
			}
		}
	}
	return d[len(s)][len(t)]
}

// TODO
func Localeconv() {

}

func Ltrim(s ...string) string {
	return trim(s, 2)
}

func Md5File(s string) string {
	f, err := os.Open(s)
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()
	h := md5.New()
	if _, err := io.Copy(h, f); err != nil {
		log.Fatal(err)
	}

	return hex.EncodeToString(h.Sum(nil))
}

func Md5(s string) string {
	h := md5.New()
	io.WriteString(h, s)
	return hex.EncodeToString(h.Sum(nil))
}

// TODO: http://php.net/manual/ja/function.metaphone.php
func Metaphone(s string) string {
	return ""
}

// TODO: http://php.net/manual/ja/function.money-format.php
func MoneyFormat(f string, m int) string {
	return ""
}

// TODO: http://php.net/manual/ja/function.nl-langinfo.php
func NlLanginfo() string {
	return ""
}

func Nl2br(s string) string {
	r := strings.NewReplacer("\n\r", "\n", "\r\n", "\n", "\r", "\n", "\n", "
\n")
	return r.Replace(s)
}

// TODO:
func NumberFormat(s string) string {
	return ""
}

func Ord(s string) int {
	return int(s[0])
}

func ParseStr(s string) map[string][]string {
	res := make(map[string][]string)
	// key=v
	queries := strings.Split(s, "&")
	for _, v := range queries {
		// 0:key , 1:v
		query := strings.Split(v, "=")
		if t := strings.Index(query[0], "[]"); t != -1 {
			// ak = key
			ak := query[0][:t]
			vv := strings.Replace(query[1], "+", " ", -1)
			res[ak] = append(res[ak], vv)
		} else {
			ak := query[0]
			res[ak] = append(res[ak], query[1])
		}
	}

	return res
}

func Print(s string) int {
	fmt.Print(s)
	return 1
}

func Printf(f string, a ...interface{}) {
	fmt.Printf(f, a...)
}

// TODO: http://php.net/manual/ja/function.quoted-printable-decode.php
func QuotedPrintableDecode(s string) string {
	return ""
}

// TODO: http://php.net/manual/ja/function.quoted-printable-encode.php
func QuotedPrintableEncode(s string) string {
	return ""
}

func Quotemeta(s string) string {
	r := strings.NewReplacer(
		`.`, `\.`,
		`\`, `\\`,
		`+`, `\+`,
		`*`, `\*`,
		`?`, `\?`,
		`[`, `\[`,
		`^`, `\^`,
		`]`, `\]`,
		`(`, `\(`,
		`$`, `\$`,
		`)`, `\)`,
	)
	return r.Replace(s)
}

func Rtrim(s ...string) string {
	return trim(s, 1)
}

// TODO: http://php.net/manual/ja/function.setlocale.php
func SetLocate(i int, l string) {
	loc := time.FixedZone(l, 9*60*60)
	time.Local = loc
}

// TODO
func Sha1File(s string) string {
	f, err := os.Open(s)
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	h := sha1.New()
	if _, err := io.Copy(h, f); err != nil {
		log.Fatal(err)
	}
	return hex.EncodeToString(h.Sum(nil))
}

func Sha1(s string) string {
	h := sha1.New()
	h.Write([]byte(s))
	return hex.EncodeToString(h.Sum(nil))
}

func trimfunc(i int) func(s string, d string) string {
	if i == 1 {
		return func(s string, d string) string {
			return strings.TrimSuffix(s, d)
		}
	} else if i == 2 {
		return func(s string, d string) string {
			return strings.TrimPrefix(s, d)
		}
	} else {
		// TODO
		return func(s string, d string) string {
			return strings.TrimPrefix(s, d)
		}
	}
}

func trim(s []string, i int) string {
	trims := trimfunc(i)
	if len(s) == 2 {
		r := strings.Split(s[1], "..")
		if len(r) == 2 {
			min := []rune(r[0])
			max := []rune(r[len(r)-1])
			str := s[0]
			t := len(str)
			for {
				for i := min[0]; i <= max[0]; i++ {
					str = trims(str, string(i))
				}
				if t == len(str) {
					break
				}
				t = len(str)
			}
			return str
		}
		return trims(s[0], s[1])
	}

	r := s[0]
	t := len(r)
	suffix := [5]string{" ", "\t", "\n", "\r", "\x0B"}

	for {
		for _, v := range suffix {
			r = trims(r, v)
		}
		if t == len(r) {
			break
		}
		t = len(r)
	}

	return r
}

func Ucfirst(s string) string {
	first := strings.ToUpper(string(s[0]))
	return first + s[1:]
}

func Lcfirst(s string) string {
	first := strings.ToLower(string(s[0]))
	return first + s[1:]
}

func Ucwords(p ...string) string {
	d := " "
	if len(p) > 1 {
		d = p[1]
	}
	words := strings.Split(p[0], d)
	var res string

	if len(words) > 1 {
		for i := 0; i < len(words); i++ {
			res += Ucfirst(words[i]) + d
		}
		res = strings.TrimSuffix(res, d)
	} else {
		res = p[0]
	}

	return res
}
<commit_msg>similartext<commit_after>package phputil

import (
	"crypto/md5"
	"crypto/sha1"
	"crypto/sha256"
	"crypto/sha512"
	"encoding/hex"
	"fmt"
	"hash/crc32"
	"html"
	"io"
	"log"
	"os"
	"strings"
	"time"

	"golang.org/x/crypto/blowfish"
)

func AddcSlashes(s string, c string) string {
	r := strings.Split(c, "..")
	var res string
	min := []rune(r[0])
	max := []rune(r[len(r)-1])
	for _, v := range s {
		if min[0] <= v && v <= max[0] {
			res += "\\" + string(v)
		} else {
			res += string(v)
		}
	}

	return res
}

func AddSlashes(s string) string {
	r := strings.NewReplacer("'", "\\'", "\"", "\\\"", "\\", "\\\\")
	return r.Replace(s)
}

func Bin2Hex(s string) string {
	b := []byte(s[:])
	return hex.EncodeToString(b)
}

func Chop(s ...string) string {
	return trim(s, 1)
}

func Chr(i int) string {
	for i < 0 {
		i += 256
	}
	i %= 256
	return string(rune(i))
}

func ChunkSplit(s string, l int, sep string) string {
	if len(s) < l {
		return s
	}

	res := s[:l] + sep
	tail := s[l:]

	for len(tail) > l {
		res += tail[:l] + sep
		tail = tail[l:]
	}
	res += tail

	return res
}

// TODO
func ConvertCyrString(s string, from string, to string) string {
	return ""
}

// TODO
func ConvertUudecode(s string) string {
	return ""
}

// TODO
func ConvertUuencode(s string) string {
	return ""
}

func CountChars(s string, i int) map[int]int {

	r := countChars(s)

	switch i {
	case 0:
		return r
	case 1:
		for n := 0; n < 255; n++ {
			if r[n] == 0 {
				delete(r, n)
			}
		}
		return r
	case 2:
		for n := 0; n < 255; n++ {
			if r[n] > 0 {
				delete(r, n)
			}
		}
		return r
	}

	return r
}

func CountChars34(s string, i int) string {
	r := countChars(s)
	var res string
	switch i {
	case 3:
		for n := 0; n < 255; n++ {
			if r[n] > 0 {
				res += string(n)
			}
		}
		return res
	case 4:
		for n := 0; n < 255; n++ {
			if r[n] == 0 {
				res += string(n)
			}
		}
		return res
	}
	return res
}

func countChars(s string) map[int]int {
	r := make(map[int]int)
	for i := 0; i < 255; i++ {
		r[i] = 0
	}

	for _, v := range s {
		r[int(v)]++
	}
	return r
}

func Crc32(s string) uint32 {
	return crc32.ChecksumIEEE([]byte(s))
}

// TODO
func Crypt(s string, salt string) string {
	var res string

	switch len(salt) {
	/**
	case 2:
		// CRYPT_STD_DES
		c, _ := des.NewCipher([]byte(salt))
		encrypted := make([]byte, des.BlockSize)
		// DES で暗号化をおこなう
		c.Encrypt(encrypted, []byte(s))
		return string(encrypted)
	case 9:
	// CRYPT_EXT_DES
	**/
	case 12:
		// CRYPT_MD5
		h := md5.New()
		h.Write([]byte(s))
		return hex.EncodeToString(h.Sum(nil))
	case 22:
		// TODO: PADDING salt. CRYPT_BLOWFISH
		cipher, _ := blowfish.NewSaltedCipher([]byte("key"), []byte(salt))
		if len(s)%blowfish.BlockSize != 0 {
			os.Exit(1)
		}
		var encrypted []byte
		cipher.Encrypt(encrypted, []byte(s))
		return string(encrypted)
	case 16:
		// TODO: if condition. CRYPT_SHA256
		if true {
			c := sha256.Sum256([]byte(s))
			return hex.EncodeToString(c[:])
		} else {
			// CRYPT_SHA51
			c := sha512.Sum512([]byte(s))
			return hex.EncodeToString(c[:])
		}

	}
	return res
}

func echo(s string) {
	fmt.Print(s)
}

func Explode(d string, s string, l int) []string {
	return strings.SplitN(s, d, l)
}

func Fprintf(w io.Writer, f string, a ...interface{}) int {
	n, _ := fmt.Fprintf(w, f, a[:]...)
	return n
}

// TODO
func GetHtmlTranslationTable(t int, f int, e string) map[string]string {
	var res map[string]string
	return res
}

// TODO
func Hebrev(s string, m int) string {
	return ""
}

// TODO
func Hebrevc(s string, m int) string {
	return ""
}

func Hex2Bin(s string) string {
	b, _ := hex.DecodeString(s)
	return string(b)
}

func HtmlEntityDecode(s string) string {
	return html.UnescapeString(s)
}

func HtmlEntities(s string) string {
	return html.EscapeString(s)
}

// TODO Fix. It's not strict.
func HtmlspecialcharsDecode(s string) string {
	return html.UnescapeString(s)
}

// TODO Fix. It's not strict.
func Htmlspecialchars(s string) string {
	return html.EscapeString(s)
}

func Implode(d string, s []string) string {
	return strings.Join(s, d)
}

func Join(d string, s []string) string {
	return Implode(d, s)
}

func Levenshtein(s string, t string) int {
	d := make([][]int, len(s)+1)
	for i := range d {
		d[i] = make([]int, len(t)+1)
	}
	for i := range d {
		d[i][0] = i
	}
	for j := range d[0] {
		d[0][j] = j
	}
	for j := 1; j <= len(t); j++ {
		for i := 1; i <= len(s); i++ {
			if s[i-1] == t[j-1] {
				d[i][j] = d[i-1][j-1]
			} else {
				min := d[i-1][j]
				if d[i][j-1] < min {
					min = d[i][j-1]
				}
				if d[i-1][j-1] < min {
					min = d[i-1][j-1]
				}
				d[i][j] = min + 1
			}
		}
	}
	return d[len(s)][len(t)]
}

// TODO
func Localeconv() {

}

func Ltrim(s ...string) string {
	return trim(s, 2)
}

func Md5File(s string) string {
	f, err := os.Open(s)
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()
	h := md5.New()
	if _, err := io.Copy(h, f); err != nil {
		log.Fatal(err)
	}

	return hex.EncodeToString(h.Sum(nil))
}

func Md5(s string) string {
	h := md5.New()
	io.WriteString(h, s)
	return hex.EncodeToString(h.Sum(nil))
}

// TODO: http://php.net/manual/ja/function.metaphone.php
func Metaphone(s string) string {
	return ""
}

// TODO: http://php.net/manual/ja/function.money-format.php
func MoneyFormat(f string, m int) string {
	return ""
}

// TODO: http://php.net/manual/ja/function.nl-langinfo.php
func NlLanginfo() string {
	return ""
}

func Nl2br(s string) string {
	r := strings.NewReplacer("\n\r", "\n", "\r\n", "\n", "\r", "\n", "\n", "
\n")
	return r.Replace(s)
}

// TODO:
func NumberFormat(s string) string {
	return ""
}

func Ord(s string) int {
	return int(s[0])
}

func ParseStr(s string) map[string][]string {
	res := make(map[string][]string)
	// key=v
	queries := strings.Split(s, "&")
	for _, v := range queries {
		// 0:key , 1:v
		query := strings.Split(v, "=")
		if t := strings.Index(query[0], "[]"); t != -1 {
			// ak = key
			ak := query[0][:t]
			vv := strings.Replace(query[1], "+", " ", -1)
			res[ak] = append(res[ak], vv)
		} else {
			ak := query[0]
			res[ak] = append(res[ak], query[1])
		}
	}

	return res
}

func Print(s string) int {
	fmt.Print(s)
	return 1
}

func Printf(f string, a ...interface{}) {
	fmt.Printf(f, a...)
}

// TODO: http://php.net/manual/ja/function.quoted-printable-decode.php
func QuotedPrintableDecode(s string) string {
	return ""
}

// TODO: http://php.net/manual/ja/function.quoted-printable-encode.php
func QuotedPrintableEncode(s string) string {
	return ""
}

func Quotemeta(s string) string {
	r := strings.NewReplacer(
		`.`, `\.`,
		`\`, `\\`,
		`+`, `\+`,
		`*`, `\*`,
		`?`, `\?`,
		`[`, `\[`,
		`^`, `\^`,
		`]`, `\]`,
		`(`, `\(`,
		`$`, `\$`,
		`)`, `\)`,
	)
	return r.Replace(s)
}

func Rtrim(s ...string) string {
	return trim(s, 1)
}

// TODO: http://php.net/manual/ja/function.setlocale.php
func SetLocate(i int, l string) {
	loc := time.FixedZone(l, 9*60*60)
	time.Local = loc
}

// TODO
func Sha1File(s string) string {
	f, err := os.Open(s)
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	h := sha1.New()
	if _, err := io.Copy(h, f); err != nil {
		log.Fatal(err)
	}
	return hex.EncodeToString(h.Sum(nil))
}

func Sha1(s string) string {
	h := sha1.New()
	h.Write([]byte(s))
	return hex.EncodeToString(h.Sum(nil))
}

// TODO http://php.net/manual/ja/function.similar-text.php
func SimilarText(f string, s string) int {
	return 0
}

func trimfunc(i int) func(s string, d string) string {
	if i == 1 {
		return func(s string, d string) string {
			return strings.TrimSuffix(s, d)
		}
	} else if i == 2 {
		return func(s string, d string) string {
			return strings.TrimPrefix(s, d)
		}
	} else {
		// TODO
		return func(s string, d string) string {
			return strings.TrimPrefix(s, d)
		}
	}
}

func trim(s []string, i int) string {
	trims := trimfunc(i)
	if len(s) == 2 {
		r := strings.Split(s[1], "..")
		if len(r) == 2 {
			min := []rune(r[0])
			max := []rune(r[len(r)-1])
			str := s[0]
			t := len(str)
			for {
				for i := min[0]; i <= max[0]; i++ {
					str = trims(str, string(i))
				}
				if t == len(str) {
					break
				}
				t = len(str)
			}
			return str
		}
		return trims(s[0], s[1])
	}

	r := s[0]
	t := len(r)
	suffix := [5]string{" ", "\t", "\n", "\r", "\x0B"}

	for {
		for _, v := range suffix {
			r = trims(r, v)
		}
		if t == len(r) {
			break
		}
		t = len(r)
	}

	return r
}

func Ucfirst(s string) string {
	first := strings.ToUpper(string(s[0]))
	return first + s[1:]
}

func Lcfirst(s string) string {
	first := strings.ToLower(string(s[0]))
	return first + s[1:]
}

func Ucwords(p ...string) string {
	d := " "
	if len(p) > 1 {
		d = p[1]
	}
	words := strings.Split(p[0], d)
	var res string

	if len(words) > 1 {
		for i := 0; i < len(words); i++ {
			res += Ucfirst(words[i]) + d
		}
		res = strings.TrimSuffix(res, d)
	} else {
		res = p[0]
	}

	return res
}
<|endoftext|>


<commit_before>package hostqueue

import (
	"appengine"
	"appengine/datastore"
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
)

/* Sample curl to create an issue:
curl --user "caspyin" -X POST --data
  '{"description":"Created via API","public":"true","files":{"file1.txt":{"content":"Demo"}}'
  https://api.github.com/gists
*/

const githubURI = "https://api.github.com/repos/AnnAddicks/hostq/issues"

type Issue struct {
	Title  string   `json:"title"`
	Body   string   `json:"body"`
	Labels []string `json:"labels"`
}

type GithubCreds struct {
	Username string `json:"username"`
	Password string `json:"username"`
}

func (creds *GithubCreds) key(c appengine.Context) *datastore.Key {
	k := datastore.NewKey(c, "GithubCreds", "default", 0, nil)
	return datastore.NewIncompleteKey(c, "GithubCreds", k)
}

func (creds *GithubCreds) save(c appengine.Context) (*GithubCreds, error) {
	_, err := datastore.Put(c, creds.key(c), creds)
	if err != nil {
		return nil, err
	}

	return creds, nil
}

func GetGithubCreds(ctx appengine.Context) (GithubCreds, error) {
	q := datastore.NewQuery("GithubCreds").Limit(1)

	var creds []GithubCreds
	var cred GithubCreds
	if _, err := q.GetAll(ctx, &creds); err != nil {
		return cred, err
	}

	return creds[0], nil
}

func CreateGithubIssueAndPanic(originalErr error, r *http.Request) {
	var issue Issue
	issue.Title = "Auto Created issue"
	issue.Body = fmt.Sprintf("%v", originalErr)
	issue.Labels = []string{"AutoGenerated Error"}

	jsonStr, _ := json.Marshal(issue)

	req, _ := http.NewRequest("POST", githubURI, bytes.NewBuffer(jsonStr))
	req.Header.Set("Content-Type", "application/json")

	ctx := appengine.NewContext(r)
	githubCreds, _ := GetGithubCreds(ctx)
	req.SetBasicAuth(githubCreds.Username, githubCreds.Password)
	handleURLFetch(req, r)

	panic(originalErr)
}
<commit_msg>added documentation.<commit_after>/* Auto Create Github issues on crashes.
 Sample curl to create an issue:
curl --user "caspyin" -X POST --data
  '{"description":"Created via API","public":"true","files":{"file1.txt":{"content":"Demo"}}'
  https://api.github.com/gists
*/
package hostqueue

import (
	"appengine"
	"appengine/datastore"
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
)

const githubURI = "https://api.github.com/repos/AnnAddicks/hostq/issues"

type Issue struct {
	Title  string   `json:"title"`
	Body   string   `json:"body"`
	Labels []string `json:"labels"`
}

type GithubCreds struct {
	Username string `json:"username"`
	Password string `json:"username"`
}

/*
	Storing creds in the datastore because app engine puts environment
	variables in app.yaml.  TODO: Cache these
*/
func GetGithubCreds(ctx appengine.Context) (GithubCreds, error) {
	q := datastore.NewQuery("GithubCreds").Limit(1)

	var creds []GithubCreds
	var cred GithubCreds
	if _, err := q.GetAll(ctx, &creds); err != nil {
		return cred, err
	}

	return creds[0], nil
}

func CreateGithubIssueAndPanic(originalErr error, r *http.Request) {
	/* TODO check the datastore first if the error has happened in the last
	30 or so days */

	var issue Issue
	issue.Title = "Auto Created issue"
	issue.Body = fmt.Sprintf("%v", originalErr)
	issue.Labels = []string{"AutoGenerated Error"}

	jsonStr, _ := json.Marshal(issue)

	req, _ := http.NewRequest("POST", githubURI, bytes.NewBuffer(jsonStr))
	req.Header.Set("Content-Type", "application/json")

	ctx := appengine.NewContext(r)
	githubCreds, _ := GetGithubCreds(ctx)
	req.SetBasicAuth(githubCreds.Username, githubCreds.Password)
	handleURLFetch(req, r)

	panic(originalErr)
}
<|endoftext|>


<commit_before>package fluidinfo

import (
	"bufio"
	"bytes"
	"encoding/base64"
	"fmt"
	"io"
	"net"
	"net/http"
	"net/url"
	"strconv"
	"strings"
)

type readClose struct {
	io.Reader
	io.Closer
}

type badStringError struct {
	what string
	str  string
}

func (e *badStringError) Error() string { return fmt.Sprintf("%s %q", e.what, e.str) }

// Given a string of the form "host", "host:port", or "[ipv6::address]:port",
// return true if the string includes a port.
func hasPort(s string) bool { return strings.LastIndex(s, ":") > strings.LastIndex(s, "]") }

func send(req *http.Request) (resp *http.Response, err error) {
	addr := req.URL.Host
	if !hasPort(addr) {
		addr += ":http"
	}
	conn, err := net.Dial("tcp", addr)
	if err != nil {
		return nil, err
	}
	err = req.Write(conn)
	if err != nil {
		conn.Close()
		return nil, err
	}
	reader := bufio.NewReader(conn)
	resp, err = http.ReadResponse(reader, req)
	if err != nil {
		conn.Close()
		return nil, err
	}
	r := io.Reader(reader)
	if v := resp.Header["Content-Length"]; v != nil {
		n, err := strconv.Atoi(v[0])
		if err != nil {
			return nil, &badStringError{"invalid Content-Length", v[0]}
		}
		v := int64(n)
		r = io.LimitReader(r, v)
	}
	resp.Body = readClose{r, conn}
	return
}

func encodedUsernameAndPassword(user, pwd string) string {
	bb := &bytes.Buffer{}
	encoder := base64.NewEncoder(base64.StdEncoding, bb)
	encoder.Write([]byte(user + ":" + pwd))
	encoder.Close()
	return bb.String()
}

// Naive utility url encode method.  Converts a string map into a query string and
// returns a string in the format "?param1=value1&param2=value2"
func UrlEncode(urlmap map[string]string) string {
	url_ := "?"
	var temp []string
	var key, value string
	for key, value = range urlmap {
		temp = append(temp, key+"="+value)
	}
	url_ += strings.Join(temp, "&")
	return url_
}

func authGet(url_, user, pwd string) (r *http.Response, err error) {
	var req http.Request
	req.Method = "GET"
	req.Header = map[string][]string{"Authorization": {"Basic " +
		encodedUsernameAndPassword(user, pwd)}}
	if req.URL, err = url.Parse(url_); err != nil {
		return
	}
	if r, err = send(&req); err != nil {
		return
	}
	return
}

// Post issues a POST to the specified URL.
// Caller should close r.Body when done reading it.
func authPost(url_, user, pwd, client, clientURL, version, agent, bodyType string,
	body io.Reader) (r *http.Response, err error) {
	var req http.Request
	req.Method = "POST"
	req.Body = body.(io.ReadCloser)
	req.Header = map[string][]string{
		"Content-Type":           {bodyType},
		"Transfer-Encoding":      {"chunked"},
		"User-Agent":             {agent},
		"X-Fluidinfo-Client":     {client},
		"X-Fluidinfo-Client-URL": {clientURL},
		"X-Fluidinfo-Version":    {version},
		"Authorization":          {"Basic " + encodedUsernameAndPassword(user, pwd)},
	}

	req.URL, err = url.Parse(url_)
	if err != nil {
		return nil, err
	}

	return send(&req)
}

// Put issues a PUT to the specified URL.
// Caller should close r.Body when done reading it.
func authPut(url_, user, pwd, client, clientURL, version, agent, bodyType string,
	body io.Reader) (r *http.Response, err error) {
	var req http.Request
	req.Method = "PUT"
	req.Body = body.(io.ReadCloser)
	if user != "" && pwd != "" {
		req.Header = map[string][]string{
			"Content-Type":           {bodyType},
			"Transfer-Encoding":      {"chunked"},
			"User-Agent":             {agent},
			"X-Fluidinfo-Client":     {client},
			"X-Fluidinfo-Client-URL": {clientURL},
			"X-Fluidinfo-Version":    {version},
			"Authorization":          {"Basic " + encodedUsernameAndPassword(user, pwd)},
		}
	} else {
		req.Header = map[string][]string{
			"Content-Type":           {bodyType},
			"Transfer-Encoding":      {"chunked"},
			"User-Agent":             {agent},
			"X-Fluidinfo-Client":     {client},
			"X-Fluidinfo-Client-URL": {clientURL},
			"X-Fluidinfo-Version":    {version},
		}
	}

	req.URL, err = url.Parse(url_)
	if err != nil {
		return nil, err
	}

	return send(&req)
}

// Delete issues a DELETE to the specified URL.
func authDelete(url_, user, pwd string) (r *http.Response, err error) {
	var req http.Request
	req.Method = "DELETE"
	if user != "" && pwd != "" {
		req.Header = map[string][]string{"Authorization": {"Basic " +
			encodedUsernameAndPassword(user, pwd)}}
	}
	if req.URL, err = url.Parse(url_); err != nil {
		return
	}
	if r, err = send(&req); err != nil {
		return
	}
	return
}

// Head issues a HEAD to the specified URL.
func authHead(url_, user, pwd string) (r *http.Response, err error) {
	var req http.Request
	req.Method = "HEAD"
	if user != "" && pwd != "" {
		req.Header = map[string][]string{"Authorization": {"Basic " +
			encodedUsernameAndPassword(user, pwd)}}
	}
	if req.URL, err = url.Parse(url_); err != nil {
		return
	}
	if r, err = send(&req); err != nil {
		return
	}
	return
}

// Do an authenticated Get if we've called Authenticated, otherwise
// just Get it without authentication.
func httpGet(url_, user, pwd string) (*http.Response, string, error) {
	var r *http.Response
	var full string = ""
	var err error
	if user != "" && pwd != "" {
		r, err = authGet(url_, user, pwd)
	} else {
		r, err = http.Get(url_)
	}
	return r, full, err
}

// Do an authenticated Post if we've called Authenticated, otherwise
// just Post it without authentication.
func httpPost(url_, user, pwd, client, clientURL, version, agent,
	data string) (*http.Response, error) {
	var r *http.Response
	var err error
	body := bytes.NewBufferString(data)
	bodyType := "application/json"
	if user != "" && pwd != "" {
		r, err = authPost(url_, user, pwd, client, clientURL,
			version, agent, bodyType, body)
	} else {
		r, err = http.Post(url_, bodyType, body)
	}
	return r, err
}

// Do an authenticated Put.
func httpPut(url_, user, pwd, client, clientURL, version, agent,
	data string) (*http.Response, error) {
	var r *http.Response
	var err error
	body := bytes.NewBufferString(data)
	bodyType := "application/json"
	r, err = authPut(url_, user, pwd, client, clientURL,
		version, agent, bodyType, body)
	return r, err
}

// Do an authenticated Delete.
func httpDelete(url_, user, pwd string) (*http.Response, error) {
	var r *http.Response
	var err error
	r, err = authDelete(url_, user, pwd)
	return r, err
}

// Do an authenticated Head.
func httpHead(url_, user, pwd string) (*http.Response, error) {
	var r *http.Response
	var err error
	r, err = authHead(url_, user, pwd)
	return r, err
}

const (
	DEFAULT_CLIENT           = "gofluidinfo"
	DEFAULT_CLIENT_URL       = "http://github.com/micrypt/gofluidinfo"
	DEFAULT_CLIENT_VERSION   = "0.1"
	DEFAULT_USER_AGENT       = "gofluidinfo"
	ERROR                    = "gofluidinfo Error: "
	WARNING                  = "gofluidinfo Warning: "
	DEFAULT_PORT             = 80
	SECURE_PORT              = 443
	UNIX_CREDENTIALS_FILE    = ".fluidinfocredentials"
	WINDOWS_CREDENTIALS_FILE = "fluidinfocredentials.ini"
	RETRY_TIMEOUT            = 5e9
	PRIMITIVE_CONTENT_TYPE   = "application/vnd.fluidinfo.value+json"
	HEADER_ERROR             = "X-Fluidinfo-Error-Class"
	HEADER_REQUEST_ID        = "X-Fluidinfo-Request-Id"
	FLUIDINFO_PATH           = "http://fluiddb.fluidinfo.com"
	SANDBOX_PATH             = "http://sandbox.fluidinfo.com"
)

type Client struct {
	Username  string
	Password  string
	URL       string
	Client    string
	ClientURL string
	Version   string
	Agent     string
}

func NewClient(username, password string) *Client {
	return &Client{username, password, SANDBOX_PATH, DEFAULT_CLIENT, DEFAULT_CLIENT_URL, DEFAULT_CLIENT_VERSION, DEFAULT_USER_AGENT}
}

func (self *Client) SetActiveMode() {
	self.URL = FLUIDINFO_PATH
}

func (self *Client) Get(url_ string) (*http.Response, error) {
	url_ = self.URL + url_
	var resp *http.Response
	var err error
	resp, _, err = httpGet(url_, self.Username, self.Password)
	return resp, err
}

func (self *Client) Post(url_, data string) (*http.Response, error) {
	url_ = self.URL + url_
	var resp *http.Response
	var err error
	resp, err = httpPost(url_, self.Username, self.Password, self.Client, self.ClientURL, self.Version, self.Agent, data)
	return resp, err
}

func (self *Client) Put(url_, data string) (*http.Response, error) {
	url_ = self.URL + url_
	var resp *http.Response
	var err error
	resp, err = httpPut(url_, self.Username, self.Password, self.Client, self.ClientURL, self.Version, self.Agent, data)
	return resp, err
}

func (self *Client) Delete(url_ string) (*http.Response, error) {
	url_ = self.URL + url_
	var resp *http.Response
	var err error
	resp, err = httpDelete(url_, self.Username, self.Password)
	return resp, err
}

func (self *Client) Head(url_ string) (*http.Response, error) {
	url_ = self.URL + url_
	var resp *http.Response
	var err error
	resp, err = httpHead(url_, self.Username, self.Password)
	return resp, err
}
<commit_msg>fluidinfo: remove outdated URL encoding function.<commit_after>package fluidinfo

import (
	"bufio"
	"bytes"
	"encoding/base64"
	"fmt"
	"io"
	"net"
	"net/http"
	"net/url"
	"strconv"
	"strings"
)

type readClose struct {
	io.Reader
	io.Closer
}

type badStringError struct {
	what string
	str  string
}

func (e *badStringError) Error() string { return fmt.Sprintf("%s %q", e.what, e.str) }

// Given a string of the form "host", "host:port", or "[ipv6::address]:port",
// return true if the string includes a port.
func hasPort(s string) bool { return strings.LastIndex(s, ":") > strings.LastIndex(s, "]") }

func send(req *http.Request) (resp *http.Response, err error) {
	addr := req.URL.Host
	if !hasPort(addr) {
		addr += ":http"
	}
	conn, err := net.Dial("tcp", addr)
	if err != nil {
		return nil, err
	}
	err = req.Write(conn)
	if err != nil {
		conn.Close()
		return nil, err
	}
	reader := bufio.NewReader(conn)
	resp, err = http.ReadResponse(reader, req)
	if err != nil {
		conn.Close()
		return nil, err
	}
	r := io.Reader(reader)
	if v := resp.Header["Content-Length"]; v != nil {
		n, err := strconv.Atoi(v[0])
		if err != nil {
			return nil, &badStringError{"invalid Content-Length", v[0]}
		}
		v := int64(n)
		r = io.LimitReader(r, v)
	}
	resp.Body = readClose{r, conn}
	return
}

func encodedUsernameAndPassword(user, pwd string) string {
	bb := &bytes.Buffer{}
	encoder := base64.NewEncoder(base64.StdEncoding, bb)
	encoder.Write([]byte(user + ":" + pwd))
	encoder.Close()
	return bb.String()
}

func authGet(url_, user, pwd string) (r *http.Response, err error) {
	var req http.Request
	req.Method = "GET"
	req.Header = map[string][]string{"Authorization": {"Basic " +
		encodedUsernameAndPassword(user, pwd)}}
	if req.URL, err = url.Parse(url_); err != nil {
		return
	}
	if r, err = send(&req); err != nil {
		return
	}
	return
}

// Post issues a POST to the specified URL.
// Caller should close r.Body when done reading it.
func authPost(url_, user, pwd, client, clientURL, version, agent, bodyType string,
	body io.Reader) (r *http.Response, err error) {
	var req http.Request
	req.Method = "POST"
	req.Body = body.(io.ReadCloser)
	req.Header = map[string][]string{
		"Content-Type":           {bodyType},
		"Transfer-Encoding":      {"chunked"},
		"User-Agent":             {agent},
		"X-Fluidinfo-Client":     {client},
		"X-Fluidinfo-Client-URL": {clientURL},
		"X-Fluidinfo-Version":    {version},
		"Authorization":          {"Basic " + encodedUsernameAndPassword(user, pwd)},
	}

	req.URL, err = url.Parse(url_)
	if err != nil {
		return nil, err
	}

	return send(&req)
}

// Put issues a PUT to the specified URL.
// Caller should close r.Body when done reading it.
func authPut(url_, user, pwd, client, clientURL, version, agent, bodyType string,
	body io.Reader) (r *http.Response, err error) {
	var req http.Request
	req.Method = "PUT"
	req.Body = body.(io.ReadCloser)
	if user != "" && pwd != "" {
		req.Header = map[string][]string{
			"Content-Type":           {bodyType},
			"Transfer-Encoding":      {"chunked"},
			"User-Agent":             {agent},
			"X-Fluidinfo-Client":     {client},
			"X-Fluidinfo-Client-URL": {clientURL},
			"X-Fluidinfo-Version":    {version},
			"Authorization":          {"Basic " + encodedUsernameAndPassword(user, pwd)},
		}
	} else {
		req.Header = map[string][]string{
			"Content-Type":           {bodyType},
			"Transfer-Encoding":      {"chunked"},
			"User-Agent":             {agent},
			"X-Fluidinfo-Client":     {client},
			"X-Fluidinfo-Client-URL": {clientURL},
			"X-Fluidinfo-Version":    {version},
		}
	}

	req.URL, err = url.Parse(url_)
	if err != nil {
		return nil, err
	}

	return send(&req)
}

// Delete issues a DELETE to the specified URL.
func authDelete(url_, user, pwd string) (r *http.Response, err error) {
	var req http.Request
	req.Method = "DELETE"
	if user != "" && pwd != "" {
		req.Header = map[string][]string{"Authorization": {"Basic " +
			encodedUsernameAndPassword(user, pwd)}}
	}
	if req.URL, err = url.Parse(url_); err != nil {
		return
	}
	if r, err = send(&req); err != nil {
		return
	}
	return
}

// Head issues a HEAD to the specified URL.
func authHead(url_, user, pwd string) (r *http.Response, err error) {
	var req http.Request
	req.Method = "HEAD"
	if user != "" && pwd != "" {
		req.Header = map[string][]string{"Authorization": {"Basic " +
			encodedUsernameAndPassword(user, pwd)}}
	}
	if req.URL, err = url.Parse(url_); err != nil {
		return
	}
	if r, err = send(&req); err != nil {
		return
	}
	return
}

// Do an authenticated Get if we've called Authenticated, otherwise
// just Get it without authentication.
func httpGet(url_, user, pwd string) (*http.Response, string, error) {
	var r *http.Response
	var full string = ""
	var err error
	if user != "" && pwd != "" {
		r, err = authGet(url_, user, pwd)
	} else {
		r, err = http.Get(url_)
	}
	return r, full, err
}

// Do an authenticated Post if we've called Authenticated, otherwise
// just Post it without authentication.
func httpPost(url_, user, pwd, client, clientURL, version, agent,
	data string) (*http.Response, error) {
	var r *http.Response
	var err error
	body := bytes.NewBufferString(data)
	bodyType := "application/json"
	if user != "" && pwd != "" {
		r, err = authPost(url_, user, pwd, client, clientURL,
			version, agent, bodyType, body)
	} else {
		r, err = http.Post(url_, bodyType, body)
	}
	return r, err
}

// Do an authenticated Put.
func httpPut(url_, user, pwd, client, clientURL, version, agent,
	data string) (*http.Response, error) {
	var r *http.Response
	var err error
	body := bytes.NewBufferString(data)
	bodyType := "application/json"
	r, err = authPut(url_, user, pwd, client, clientURL,
		version, agent, bodyType, body)
	return r, err
}

// Do an authenticated Delete.
func httpDelete(url_, user, pwd string) (*http.Response, error) {
	var r *http.Response
	var err error
	r, err = authDelete(url_, user, pwd)
	return r, err
}

// Do an authenticated Head.
func httpHead(url_, user, pwd string) (*http.Response, error) {
	var r *http.Response
	var err error
	r, err = authHead(url_, user, pwd)
	return r, err
}

const (
	DEFAULT_CLIENT           = "gofluidinfo"
	DEFAULT_CLIENT_URL       = "http://github.com/micrypt/gofluidinfo"
	DEFAULT_CLIENT_VERSION   = "0.1"
	DEFAULT_USER_AGENT       = "gofluidinfo"
	ERROR                    = "gofluidinfo Error: "
	WARNING                  = "gofluidinfo Warning: "
	DEFAULT_PORT             = 80
	SECURE_PORT              = 443
	UNIX_CREDENTIALS_FILE    = ".fluidinfocredentials"
	WINDOWS_CREDENTIALS_FILE = "fluidinfocredentials.ini"
	RETRY_TIMEOUT            = 5e9
	PRIMITIVE_CONTENT_TYPE   = "application/vnd.fluidinfo.value+json"
	HEADER_ERROR             = "X-Fluidinfo-Error-Class"
	HEADER_REQUEST_ID        = "X-Fluidinfo-Request-Id"
	FLUIDINFO_PATH           = "http://fluiddb.fluidinfo.com"
	SANDBOX_PATH             = "http://sandbox.fluidinfo.com"
)

type Client struct {
	Username  string
	Password  string
	URL       string
	Client    string
	ClientURL string
	Version   string
	Agent     string
}

func NewClient(username, password string) *Client {
	return &Client{username, password, SANDBOX_PATH, DEFAULT_CLIENT, DEFAULT_CLIENT_URL, DEFAULT_CLIENT_VERSION, DEFAULT_USER_AGENT}
}

func (self *Client) SetActiveMode() {
	self.URL = FLUIDINFO_PATH
}

func (self *Client) Get(url_ string) (*http.Response, error) {
	url_ = self.URL + url_
	var resp *http.Response
	var err error
	resp, _, err = httpGet(url_, self.Username, self.Password)
	return resp, err
}

func (self *Client) Post(url_, data string) (*http.Response, error) {
	url_ = self.URL + url_
	var resp *http.Response
	var err error
	resp, err = httpPost(url_, self.Username, self.Password, self.Client, self.ClientURL, self.Version, self.Agent, data)
	return resp, err
}

func (self *Client) Put(url_, data string) (*http.Response, error) {
	url_ = self.URL + url_
	var resp *http.Response
	var err error
	resp, err = httpPut(url_, self.Username, self.Password, self.Client, self.ClientURL, self.Version, self.Agent, data)
	return resp, err
}

func (self *Client) Delete(url_ string) (*http.Response, error) {
	url_ = self.URL + url_
	var resp *http.Response
	var err error
	resp, err = httpDelete(url_, self.Username, self.Password)
	return resp, err
}

func (self *Client) Head(url_ string) (*http.Response, error) {
	url_ = self.URL + url_
	var resp *http.Response
	var err error
	resp, err = httpHead(url_, self.Username, self.Password)
	return resp, err
}
<|endoftext|>


<commit_before>package main

import "fmt"

func main() {
    x := "hello world"
    fmt.Printf("Go compiler is able to infer the type based on the literal value you assign the variable\n")
    fmt.Printf("x=%s \n", x);

}
<commit_msg>add a new variable out of function<commit_after>package main

import "fmt"

var dogName string = "Max"

func main() {
    x := "hello world"
    fmt.Printf("Go compiler is able to infer the type based on the literal value you assign the variable\n")
    fmt.Printf("x=%s \n", x);

    fmt.Printf("dogName = %s\n", dogName)
}
<|endoftext|>


<commit_before>// Copyright 2009 The Go 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 list implements a doubly linked list.
//
// To iterate over a list (where l is a *List):
//	for e := l.Front(); e != nil; e = e.Next() {
//		// do something with e.Value
//	}
//
package list

// Element is an element of a linked list.
type Element struct {
	// Next and previous pointers in the doubly-linked list of elements.
	// To simplify the implementation, internally a list l is implemented
	// as a ring, such that &l.root is both the next element of the last
	// list element (l.Back()) and the previous element of the first list
	// element (l.Front()).
	next, prev *Element

	// The list to which this element belongs.
	list *List

	// The value stored with this element.
	Value interface{}
}

// Next returns the next list element or nil.
func (e *Element) Next() *Element {
	if p := e.next; p != &e.list.root {
		return p
	}
	return nil
}

// Prev returns the previous list element or nil.
func (e *Element) Prev() *Element {
	if p := e.prev; p != &e.list.root {
		return p
	}
	return nil
}

// List represents a doubly linked list.
// The zero value for List is an empty list ready to use.
type List struct {
	root Element // sentinel list element, only &root, root.prev, and root.next are used
	len  int     // current list length excluding (this) sentinel element
}

// Init initializes or clears list l.
func (l *List) Init() *List {
	l.root.next = &l.root
	l.root.prev = &l.root
	l.len = 0
	return l
}

// New returns an initialized list.
func New() *List { return new(List).Init() }

// Len returns the number of elements of list l.
func (l *List) Len() int { return l.len }

// Front returns the first element of list l or nil
func (l *List) Front() *Element {
	if l.len == 0 {
		return nil
	}
	return l.root.next
}

// Back returns the last element of list l or nil.
func (l *List) Back() *Element {
	if l.len == 0 {
		return nil
	}
	return l.root.prev
}

// lazyInit lazily initializes a zero List value.
func (l *List) lazyInit() {
	if l.root.next == nil {
		l.Init()
	}
}

// insert inserts e after at, increments l.len, and returns e.
func (l *List) insert(e, at *Element) *Element {
	n := at.next
	at.next = e
	e.prev = at
	e.next = n
	n.prev = e
	e.list = l
	l.len++
	return e
}

// insertValue is a convenience wrapper for insert(&Element{Value: v}, at).
func (l *List) insertValue(v interface{}, at *Element) *Element {
	return l.insert(&Element{Value: v}, at)
}

// remove removes e from its list, decrements l.len, and returns e.
func (l *List) remove(e *Element) *Element {
	e.prev.next = e.next
	e.next.prev = e.prev
	e.list = nil
	l.len--
	return e
}

// Remove removes e from l if e is an element of list l.
// It returns the element value e.Value.
func (l *List) Remove(e *Element) interface{} {
	if e.list == l {
		// if e.list == l, l must have been initialized when e was inserted
		// in l or l == nil (e is a zero Element) and l.remove will crash
		l.remove(e)
	}
	return e.Value
}

// Pushfront inserts a new element e with value v at the front of list l and returns e.
func (l *List) PushFront(v interface{}) *Element {
	l.lazyInit()
	return l.insertValue(v, &l.root)
}

// PushBack inserts a new element e with value v at the back of list l and returns e.
func (l *List) PushBack(v interface{}) *Element {
	l.lazyInit()
	return l.insertValue(v, l.root.prev)
}

// InsertBefore inserts a new element e with value v immediately before mark and returns e.
// If mark is not an element of l, the list is not modified.
func (l *List) InsertBefore(v interface{}, mark *Element) *Element {
	if mark.list != l {
		return nil
	}
	// see comment in List.Remove about initialization of l
	return l.insertValue(v, mark.prev)
}

// InsertAfter inserts a new element e with value v immediately after mark and returns e.
// If mark is not an element of l, the list is not modified.
func (l *List) InsertAfter(v interface{}, mark *Element) *Element {
	if mark.list != l {
		return nil
	}
	// see comment in List.Remove about initialization of l
	return l.insertValue(v, mark)
}

// MoveToFront moves element e to the front of list l.
// If e is not an element of l, the list is not modified.
func (l *List) MoveToFront(e *Element) {
	if e.list != l || l.root.next == e {
		return
	}
	// see comment in List.Remove about initialization of l
	l.insert(l.remove(e), &l.root)
}

// MoveToBack moves element e to the back of list l.
// If e is not an element of l, the list is not modified.
func (l *List) MoveToBack(e *Element) {
	if e.list != l || l.root.prev == e {
		return
	}
	// see comment in List.Remove about initialization of l
	l.insert(l.remove(e), l.root.prev)
}

// PushBackList inserts a copy of an other list at the back of list l.
// The lists l and other may be the same.
func (l *List) PushBackList(other *List) {
	l.lazyInit()
	for i, e := other.Len(), other.Front(); i > 0; i, e = i-1, e.Next() {
		l.insertValue(e.Value, l.root.prev)
	}
}

// PushFrontList inserts a copy of an other list at the front of list l.
// The lists l and other may be the same.
func (l *List) PushFrontList(other *List) {
	l.lazyInit()
	for i, e := other.Len(), other.Back(); i > 0; i, e = i-1, e.Prev() {
		l.insertValue(e.Value, &l.root)
	}
}
<commit_msg>container/list: avoid memory leaks<commit_after>// Copyright 2009 The Go 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 list implements a doubly linked list.
//
// To iterate over a list (where l is a *List):
//	for e := l.Front(); e != nil; e = e.Next() {
//		// do something with e.Value
//	}
//
package list

// Element is an element of a linked list.
type Element struct {
	// Next and previous pointers in the doubly-linked list of elements.
	// To simplify the implementation, internally a list l is implemented
	// as a ring, such that &l.root is both the next element of the last
	// list element (l.Back()) and the previous element of the first list
	// element (l.Front()).
	next, prev *Element

	// The list to which this element belongs.
	list *List

	// The value stored with this element.
	Value interface{}
}

// Next returns the next list element or nil.
func (e *Element) Next() *Element {
	if p := e.next; p != &e.list.root {
		return p
	}
	return nil
}

// Prev returns the previous list element or nil.
func (e *Element) Prev() *Element {
	if p := e.prev; p != &e.list.root {
		return p
	}
	return nil
}

// List represents a doubly linked list.
// The zero value for List is an empty list ready to use.
type List struct {
	root Element // sentinel list element, only &root, root.prev, and root.next are used
	len  int     // current list length excluding (this) sentinel element
}

// Init initializes or clears list l.
func (l *List) Init() *List {
	l.root.next = &l.root
	l.root.prev = &l.root
	l.len = 0
	return l
}

// New returns an initialized list.
func New() *List { return new(List).Init() }

// Len returns the number of elements of list l.
func (l *List) Len() int { return l.len }

// Front returns the first element of list l or nil
func (l *List) Front() *Element {
	if l.len == 0 {
		return nil
	}
	return l.root.next
}

// Back returns the last element of list l or nil.
func (l *List) Back() *Element {
	if l.len == 0 {
		return nil
	}
	return l.root.prev
}

// lazyInit lazily initializes a zero List value.
func (l *List) lazyInit() {
	if l.root.next == nil {
		l.Init()
	}
}

// insert inserts e after at, increments l.len, and returns e.
func (l *List) insert(e, at *Element) *Element {
	n := at.next
	at.next = e
	e.prev = at
	e.next = n
	n.prev = e
	e.list = l
	l.len++
	return e
}

// insertValue is a convenience wrapper for insert(&Element{Value: v}, at).
func (l *List) insertValue(v interface{}, at *Element) *Element {
	return l.insert(&Element{Value: v}, at)
}

// remove removes e from its list, decrements l.len, and returns e.
func (l *List) remove(e *Element) *Element {
	e.prev.next = e.next
	e.next.prev = e.prev
	e.next = nil // avoid memory leaks
	e.prev = nil // avoid memory leaks
	e.list = nil
	l.len--
	return e
}

// Remove removes e from l if e is an element of list l.
// It returns the element value e.Value.
func (l *List) Remove(e *Element) interface{} {
	if e.list == l {
		// if e.list == l, l must have been initialized when e was inserted
		// in l or l == nil (e is a zero Element) and l.remove will crash
		l.remove(e)
	}
	return e.Value
}

// Pushfront inserts a new element e with value v at the front of list l and returns e.
func (l *List) PushFront(v interface{}) *Element {
	l.lazyInit()
	return l.insertValue(v, &l.root)
}

// PushBack inserts a new element e with value v at the back of list l and returns e.
func (l *List) PushBack(v interface{}) *Element {
	l.lazyInit()
	return l.insertValue(v, l.root.prev)
}

// InsertBefore inserts a new element e with value v immediately before mark and returns e.
// If mark is not an element of l, the list is not modified.
func (l *List) InsertBefore(v interface{}, mark *Element) *Element {
	if mark.list != l {
		return nil
	}
	// see comment in List.Remove about initialization of l
	return l.insertValue(v, mark.prev)
}

// InsertAfter inserts a new element e with value v immediately after mark and returns e.
// If mark is not an element of l, the list is not modified.
func (l *List) InsertAfter(v interface{}, mark *Element) *Element {
	if mark.list != l {
		return nil
	}
	// see comment in List.Remove about initialization of l
	return l.insertValue(v, mark)
}

// MoveToFront moves element e to the front of list l.
// If e is not an element of l, the list is not modified.
func (l *List) MoveToFront(e *Element) {
	if e.list != l || l.root.next == e {
		return
	}
	// see comment in List.Remove about initialization of l
	l.insert(l.remove(e), &l.root)
}

// MoveToBack moves element e to the back of list l.
// If e is not an element of l, the list is not modified.
func (l *List) MoveToBack(e *Element) {
	if e.list != l || l.root.prev == e {
		return
	}
	// see comment in List.Remove about initialization of l
	l.insert(l.remove(e), l.root.prev)
}

// PushBackList inserts a copy of an other list at the back of list l.
// The lists l and other may be the same.
func (l *List) PushBackList(other *List) {
	l.lazyInit()
	for i, e := other.Len(), other.Front(); i > 0; i, e = i-1, e.Next() {
		l.insertValue(e.Value, l.root.prev)
	}
}

// PushFrontList inserts a copy of an other list at the front of list l.
// The lists l and other may be the same.
func (l *List) PushFrontList(other *List) {
	l.lazyInit()
	for i, e := other.Len(), other.Back(); i > 0; i, e = i-1, e.Prev() {
		l.insertValue(e.Value, &l.root)
	}
}
<|endoftext|>


<commit_before>// Copyright 2013 Artur Grabowski. All rights reserved.
// Use of this source code is governed by a ISC-style
// license that can be found in the LICENSE file.
package timers_test

import (
	"github.com/art4711/timers"
	"testing"
	"time"
)

const q = time.Millisecond * 200
const qd = 5.0

func af(t *testing.T, h time.Duration, e float64, name string) {
	e /= qd
	aff(t, h, e, name)
}

func aff(t *testing.T, h time.Duration, e float64, name string) {
	if h.Seconds() < e || h.Seconds() > e + 0.02 {
		t.Errorf("bad %v: %v !~= %v", name, h, e)
	}
}

func ai(t *testing.T, h, e int64, name string) {
	if h != e {
		t.Errorf("bad %v: %v !~= %v", name, h, e)
	}
}

func nam(n []string) string {
	var r string
	for k, v := range n {
		if k > 0 {
			r += "." + v
		} else {
			r = v
		}
	}
	return r
}

func TestBasic(t *testing.T) {
	tm := timers.New()
	t1 := tm.Start("first")
	time.Sleep(q)
	t1.Stop()
	count := 0
	tm.Foreach(func (na []string, tot, a, mx, mi time.Duration, c int64) {
		n := nam(na)
		if n != "first" {
			t.Errorf("bad name: %v", n)
		}
		af(t, tot, 1.0, "tot")
		af(t, a, 1.0, "a")
		af(t, mx, 1.0, "mx")
		af(t, mi, 1.0, "mi")
		ai(t, c, 1, "c")
		count++
	})
	if count != 1 {
		t.Errorf("too many timers %v", count)
	}
}

func TestNested(t *testing.T) {
	tm := timers.New()
	t1 := tm.Start("first")
	time.Sleep(q)
	t2 := t1.Start("first")
	time.Sleep(q)
	t2.Stop()
	t1.Stop()
	count := 0
	tm.Foreach(func (na []string, tot, a, mx, mi time.Duration, c int64) {
		n := nam(na)
		switch n {
		case "first":
			af(t, tot, 2.0, n + ".tot")
		case "first.first":
			af(t, tot, 1.0, n + ".tot")
		default:
			t.Errorf("bad name: %v", n)
		}
		aff(t, a, tot.Seconds(), n + ".a")
		aff(t, mx, tot.Seconds(), n + ".mx")
		aff(t, mi, tot.Seconds(), n + ".mi")
		ai(t, c, 1, n + ".c")
		count++
	})
	if count != 2 {
		t.Errorf("too many timers %v", count)
	}
}

func TestRepeat(t *testing.T) {
	tm := timers.New()
	t1 := tm.Start("1")
	time.Sleep(q)
	t1.Stop()

	t1 = tm.Start("1")
	time.Sleep(q * 2)
	t1.Stop()

	count := 0
	tm.Foreach(func (na []string, tot, a, mx, mi time.Duration, c int64) {
		n := nam(na)
		switch n {
		case "1":
			if c != 2 {
				t.Errorf("bad count: %v", c)
			}
		default:
			t.Errorf("bad name: %v", n)
		}
		af(t, mi, 1.0, "mi")
		af(t, mx, 2.0, "mx")
		af(t, tot, 3.0, "tot")
		af(t, a, 1.5, "avg")
		count++
	})
	if count != 1 {
		t.Errorf("too many timers %v", count)
	}
}

func TestNestedHandover(t *testing.T) {
	tm := timers.New()
	t1 := tm.Start("1")
	time.Sleep(q)
	t2 := t1.Start("1")
	time.Sleep(q)
	t2 = t2.Handover("2")
	time.Sleep(q)
	t2 = t2.Handover("1")
	time.Sleep(q * 2)
	t2.Stop()
	t1.Stop()
	count := 0
	tm.Foreach(func (na []string, tot, a, mx, mi time.Duration, c int64) {
		n := nam(na)
		switch n {
		case "1":
			af(t, tot, 5.0, n + ".tot")
			af(t, mx, 5.0, n + ".mx")
			af(t, mi, 5.0, n + ".mi")
			ai(t, c, 1, n + ".c")
		case "1.1":
			af(t, tot, 3.0, n + ".tot")
			af(t, mx, 2.0, n + ".mx")
			af(t, mi, 1.0, n + ".mi")
			ai(t, c, 2, n + ".c")
		case "1.2":
			af(t, tot, 1.0, n + ".tot")
			af(t, mx, 1.0, n + ".mx")
			af(t, mi, 1.0, n + ".mi")
			ai(t, c, 1, n + ".c")
		default:
			t.Errorf("bad name: %v", n)
		}
		count++
	})
	if count != 3 {
		t.Errorf("too many timers %v", count)
	}
}

func tfun(t2 *timers.Event) {
	t2 = t2.Handover("2")
	time.Sleep(q)
	t2 = t2.Handover("1")
	time.Sleep(q * 2)
	t2.Stop()
}

func TestNestedHandoverFunction(t *testing.T) {
	tm := timers.New()
	t1 := tm.Start("1")
	time.Sleep(q)
	t2 := t1.Start("1")
	time.Sleep(q)
	tfun(t2)
	t1.Stop()
	count := 0
	tm.Foreach(func (na []string, tot, a, mx, mi time.Duration, c int64) {
		n := nam(na)
		switch n {
		case "1":
			af(t, tot, 5.0, n + ".tot")
			af(t, mx, 5.0, n + ".mx")
			af(t, mi, 5.0, n + ".mi")
			ai(t, c, 1, n + ".c")
		case "1.1":
			af(t, tot, 3.0, n + ".tot")
			af(t, mx, 2.0, n + ".mx")
			af(t, mi, 1.0, n + ".mi")
			ai(t, c, 2, n + ".c")
		case "1.2":
			af(t, tot, 1.0, n + ".tot")
			af(t, mx, 1.0, n + ".mx")
			af(t, mi, 1.0, n + ".mi")
			ai(t, c, 1, n + ".c")
		default:
			t.Errorf("bad name: %v", n)
		}
		count++
	})
	if count != 3 {
		t.Errorf("too many timers %v", count)
	}
}
<commit_msg>Basic benchmark.<commit_after>// Copyright 2013 Artur Grabowski. All rights reserved.
// Use of this source code is governed by a ISC-style
// license that can be found in the LICENSE file.
package timers_test

import (
	"github.com/art4711/timers"
	"testing"
	"time"
)

const q = time.Millisecond * 200
const qd = 5.0

func af(t *testing.T, h time.Duration, e float64, name string) {
	e /= qd
	aff(t, h, e, name)
}

func aff(t *testing.T, h time.Duration, e float64, name string) {
	if h.Seconds() < e || h.Seconds() > e + 0.02 {
		t.Errorf("bad %v: %v !~= %v", name, h, e)
	}
}

func ai(t *testing.T, h, e int64, name string) {
	if h != e {
		t.Errorf("bad %v: %v !~= %v", name, h, e)
	}
}

func nam(n []string) string {
	var r string
	for k, v := range n {
		if k > 0 {
			r += "." + v
		} else {
			r = v
		}
	}
	return r
}

func TestBasic(t *testing.T) {
	tm := timers.New()
	t1 := tm.Start("first")
	time.Sleep(q)
	t1.Stop()
	count := 0
	tm.Foreach(func (na []string, tot, a, mx, mi time.Duration, c int64) {
		n := nam(na)
		if n != "first" {
			t.Errorf("bad name: %v", n)
		}
		af(t, tot, 1.0, "tot")
		af(t, a, 1.0, "a")
		af(t, mx, 1.0, "mx")
		af(t, mi, 1.0, "mi")
		ai(t, c, 1, "c")
		count++
	})
	if count != 1 {
		t.Errorf("too many timers %v", count)
	}
}

func TestNested(t *testing.T) {
	tm := timers.New()
	t1 := tm.Start("first")
	time.Sleep(q)
	t2 := t1.Start("first")
	time.Sleep(q)
	t2.Stop()
	t1.Stop()
	count := 0
	tm.Foreach(func (na []string, tot, a, mx, mi time.Duration, c int64) {
		n := nam(na)
		switch n {
		case "first":
			af(t, tot, 2.0, n + ".tot")
		case "first.first":
			af(t, tot, 1.0, n + ".tot")
		default:
			t.Errorf("bad name: %v", n)
		}
		aff(t, a, tot.Seconds(), n + ".a")
		aff(t, mx, tot.Seconds(), n + ".mx")
		aff(t, mi, tot.Seconds(), n + ".mi")
		ai(t, c, 1, n + ".c")
		count++
	})
	if count != 2 {
		t.Errorf("too many timers %v", count)
	}
}

func TestRepeat(t *testing.T) {
	tm := timers.New()
	t1 := tm.Start("1")
	time.Sleep(q)
	t1.Stop()

	t1 = tm.Start("1")
	time.Sleep(q * 2)
	t1.Stop()

	count := 0
	tm.Foreach(func (na []string, tot, a, mx, mi time.Duration, c int64) {
		n := nam(na)
		switch n {
		case "1":
			if c != 2 {
				t.Errorf("bad count: %v", c)
			}
		default:
			t.Errorf("bad name: %v", n)
		}
		af(t, mi, 1.0, "mi")
		af(t, mx, 2.0, "mx")
		af(t, tot, 3.0, "tot")
		af(t, a, 1.5, "avg")
		count++
	})
	if count != 1 {
		t.Errorf("too many timers %v", count)
	}
}

func TestNestedHandover(t *testing.T) {
	tm := timers.New()
	t1 := tm.Start("1")
	time.Sleep(q)
	t2 := t1.Start("1")
	time.Sleep(q)
	t2 = t2.Handover("2")
	time.Sleep(q)
	t2 = t2.Handover("1")
	time.Sleep(q * 2)
	t2.Stop()
	t1.Stop()
	count := 0
	tm.Foreach(func (na []string, tot, a, mx, mi time.Duration, c int64) {
		n := nam(na)
		switch n {
		case "1":
			af(t, tot, 5.0, n + ".tot")
			af(t, mx, 5.0, n + ".mx")
			af(t, mi, 5.0, n + ".mi")
			ai(t, c, 1, n + ".c")
		case "1.1":
			af(t, tot, 3.0, n + ".tot")
			af(t, mx, 2.0, n + ".mx")
			af(t, mi, 1.0, n + ".mi")
			ai(t, c, 2, n + ".c")
		case "1.2":
			af(t, tot, 1.0, n + ".tot")
			af(t, mx, 1.0, n + ".mx")
			af(t, mi, 1.0, n + ".mi")
			ai(t, c, 1, n + ".c")
		default:
			t.Errorf("bad name: %v", n)
		}
		count++
	})
	if count != 3 {
		t.Errorf("too many timers %v", count)
	}
}

func tfun(t2 *timers.Event) {
	t2 = t2.Handover("2")
	time.Sleep(q)
	t2 = t2.Handover("1")
	time.Sleep(q * 2)
	t2.Stop()
}

func TestNestedHandoverFunction(t *testing.T) {
	tm := timers.New()
	t1 := tm.Start("1")
	time.Sleep(q)
	t2 := t1.Start("1")
	time.Sleep(q)
	tfun(t2)
	t1.Stop()
	count := 0
	tm.Foreach(func (na []string, tot, a, mx, mi time.Duration, c int64) {
		n := nam(na)
		switch n {
		case "1":
			af(t, tot, 5.0, n + ".tot")
			af(t, mx, 5.0, n + ".mx")
			af(t, mi, 5.0, n + ".mi")
			ai(t, c, 1, n + ".c")
		case "1.1":
			af(t, tot, 3.0, n + ".tot")
			af(t, mx, 2.0, n + ".mx")
			af(t, mi, 1.0, n + ".mi")
			ai(t, c, 2, n + ".c")
		case "1.2":
			af(t, tot, 1.0, n + ".tot")
			af(t, mx, 1.0, n + ".mx")
			af(t, mi, 1.0, n + ".mi")
			ai(t, c, 1, n + ".c")
		default:
			t.Errorf("bad name: %v", n)
		}
		count++
	})
	if count != 3 {
		t.Errorf("too many timers %v", count)
	}
}

func BenchmarkBasic(b *testing.B) {
	for i:= 0; i < b.N; i++ {
		tm := timers.New()
		t1 := tm.Start("1")
		t2 := t1.Start("1")
		t2 = t2.Handover("2")
		t2 = t2.Handover("1")
		t2.Stop()
		t1.Handover("2")
		t1.Stop()
	}
}
<|endoftext|>


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

// This file implements API tests across platforms and will never have a build
// tag.

package net

import (
	"os"
	"runtime"
	"strings"
	"testing"
	"time"
)

func strfunc(s string) func() string {
	return func() string {
		return s
	}
}

var packetConnTests = []struct {
	net   string
	addr1 func() string
	addr2 func() string
}{
	{"udp", strfunc("127.0.0.1:0"), strfunc("127.0.0.1:0")},
	{"ip:icmp", strfunc("127.0.0.1"), strfunc("127.0.0.1")},
	{"unixgram", testUnixAddr, testUnixAddr},
}

func TestPacketConn(t *testing.T) {
	closer := func(c PacketConn, net, addr1, addr2 string) {
		c.Close()
		switch net {
		case "unixgram":
			os.Remove(addr1)
			os.Remove(addr2)
		}
	}

	for i, tt := range packetConnTests {
		var wb []byte
		netstr := strings.Split(tt.net, ":")
		switch netstr[0] {
		case "udp":
			wb = []byte("UDP PACKETCONN TEST")
		case "ip":
			switch runtime.GOOS {
			case "plan9":
				continue
			}
			if os.Getuid() != 0 {
				continue
			}
			var err error
			wb, err = (&icmpMessage{
				Type: icmpv4EchoRequest, Code: 0,
				Body: &icmpEcho{
					ID: os.Getpid() & 0xffff, Seq: i + 1,
					Data: []byte("IP PACKETCONN TEST"),
				},
			}).Marshal()
			if err != nil {
				t.Fatalf("icmpMessage.Marshal failed: %v", err)
			}
		case "unixgram":
			switch runtime.GOOS {
			case "plan9", "windows":
				continue
			}
			wb = []byte("UNIXGRAM PACKETCONN TEST")
		default:
			continue
		}

		addr1, addr2 := tt.addr1(), tt.addr2()
		c1, err := ListenPacket(tt.net, addr1)
		if err != nil {
			t.Fatalf("ListenPacket failed: %v", err)
		}
		defer closer(c1, netstr[0], addr1, addr2)
		c1.LocalAddr()
		c1.SetDeadline(time.Now().Add(100 * time.Millisecond))
		c1.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
		c1.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))

		c2, err := ListenPacket(tt.net, addr2)
		if err != nil {
			t.Fatalf("ListenPacket failed: %v", err)
		}
		defer closer(c2, netstr[0], addr1, addr2)
		c2.LocalAddr()
		c2.SetDeadline(time.Now().Add(100 * time.Millisecond))
		c2.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
		c2.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))

		if _, err := c1.WriteTo(wb, c2.LocalAddr()); err != nil {
			t.Fatalf("PacketConn.WriteTo failed: %v", err)
		}
		rb2 := make([]byte, 128)
		if _, _, err := c2.ReadFrom(rb2); err != nil {
			t.Fatalf("PacketConn.ReadFrom failed: %v", err)
		}
		if _, err := c2.WriteTo(wb, c1.LocalAddr()); err != nil {
			t.Fatalf("PacketConn.WriteTo failed: %v", err)
		}
		rb1 := make([]byte, 128)
		if _, _, err := c1.ReadFrom(rb1); err != nil {
			t.Fatalf("PacketConn.ReadFrom failed: %v", err)
		}
	}
}

func TestConnAndPacketConn(t *testing.T) {
	closer := func(c PacketConn, net, addr1, addr2 string) {
		c.Close()
		switch net {
		case "unixgram":
			os.Remove(addr1)
			os.Remove(addr2)
		}
	}

	for i, tt := range packetConnTests {
		var wb []byte
		netstr := strings.Split(tt.net, ":")
		switch netstr[0] {
		case "udp":
			wb = []byte("UDP PACKETCONN TEST")
		case "ip":
			switch runtime.GOOS {
			case "plan9":
				continue
			}
			if os.Getuid() != 0 {
				continue
			}
			var err error
			wb, err = (&icmpMessage{
				Type: icmpv4EchoRequest, Code: 0,
				Body: &icmpEcho{
					ID: os.Getpid() & 0xffff, Seq: i + 1,
					Data: []byte("IP PACKETCONN TEST"),
				},
			}).Marshal()
			if err != nil {
				t.Fatalf("icmpMessage.Marshal failed: %v", err)
			}
		case "unixgram":
			switch runtime.GOOS {
			case "plan9", "windows":
				continue
			}
			wb = []byte("UNIXGRAM PACKETCONN TEST")
		default:
			continue
		}

		addr1, addr2 := tt.addr1(), tt.addr2()
		c1, err := ListenPacket(tt.net, addr1)
		if err != nil {
			t.Fatalf("ListenPacket failed: %v", err)
		}
		defer closer(c1, netstr[0], addr1, addr2)
		c1.LocalAddr()
		c1.SetDeadline(time.Now().Add(100 * time.Millisecond))
		c1.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
		c1.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))

		c2, err := Dial(tt.net, c1.LocalAddr().String())
		if err != nil {
			t.Fatalf("Dial failed: %v", err)
		}
		defer c2.Close()
		c2.LocalAddr()
		c2.RemoteAddr()
		c2.SetDeadline(time.Now().Add(100 * time.Millisecond))
		c2.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
		c2.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))

		if _, err := c2.Write(wb); err != nil {
			t.Fatalf("Conn.Write failed: %v", err)
		}
		rb1 := make([]byte, 128)
		if _, _, err := c1.ReadFrom(rb1); err != nil {
			t.Fatalf("PacetConn.ReadFrom failed: %v", err)
		}
		var dst Addr
		switch netstr[0] {
		case "ip":
			dst = &IPAddr{IP: IPv4(127, 0, 0, 1)}
		case "unixgram":
			continue
		default:
			dst = c2.LocalAddr()
		}
		if _, err := c1.WriteTo(wb, dst); err != nil {
			t.Fatalf("PacketConn.WriteTo failed: %v", err)
		}
		rb2 := make([]byte, 128)
		if _, err := c2.Read(rb2); err != nil {
			t.Fatalf("Conn.Read failed: %v", err)
		}
	}
}
<commit_msg>net: enable PacketConn test for raw IP network on Windows<commit_after>// Copyright 2012 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// This file implements API tests across platforms and will never have a build
// tag.

package net

import (
	"os"
	"runtime"
	"strings"
	"testing"
	"time"
)

func strfunc(s string) func() string {
	return func() string {
		return s
	}
}

func packetConnTestData(t *testing.T, net string, i int) ([]byte, func()) {
	switch net {
	case "udp":
		return []byte("UDP PACKETCONN TEST"), nil
	case "ip":
		switch runtime.GOOS {
		case "plan9":
			return nil, func() {
				t.Logf("skipping %q test on %q", net, runtime.GOOS)
			}
		case "windows":
		default:
			if os.Getuid() != 0 {
				return nil, func() {
					t.Logf("skipping %q test; must be root", net)
				}
			}
		}
		b, err := (&icmpMessage{
			Type: icmpv4EchoRequest, Code: 0,
			Body: &icmpEcho{
				ID: os.Getpid() & 0xffff, Seq: i + 1,
				Data: []byte("IP PACKETCONN TEST"),
			},
		}).Marshal()
		if err != nil {
			return nil, func() {
				t.Fatalf("icmpMessage.Marshal failed: %v", err)
			}
		}
		return b, nil
	case "unixgram":
		switch runtime.GOOS {
		case "plan9", "windows":
			return nil, func() {
				t.Logf("skipping %q test on %q", net, runtime.GOOS)
			}
		default:
			return []byte("UNIXGRAM PACKETCONN TEST"), nil
		}
	default:
		return nil, func() {
			t.Logf("skipping %q test", net)
		}
	}
}

var packetConnTests = []struct {
	net   string
	addr1 func() string
	addr2 func() string
}{
	{"udp", strfunc("127.0.0.1:0"), strfunc("127.0.0.1:0")},
	{"ip:icmp", strfunc("127.0.0.1"), strfunc("127.0.0.1")},
	{"unixgram", testUnixAddr, testUnixAddr},
}

func TestPacketConn(t *testing.T) {
	closer := func(c PacketConn, net, addr1, addr2 string) {
		c.Close()
		switch net {
		case "unixgram":
			os.Remove(addr1)
			os.Remove(addr2)
		}
	}

	for i, tt := range packetConnTests {
		netstr := strings.Split(tt.net, ":")
		wb, skipOrFatalFn := packetConnTestData(t, netstr[0], i)
		if skipOrFatalFn != nil {
			skipOrFatalFn()
			continue
		}

		addr1, addr2 := tt.addr1(), tt.addr2()
		c1, err := ListenPacket(tt.net, addr1)
		if err != nil {
			t.Fatalf("ListenPacket failed: %v", err)
		}
		defer closer(c1, netstr[0], addr1, addr2)
		c1.LocalAddr()
		c1.SetDeadline(time.Now().Add(100 * time.Millisecond))
		c1.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
		c1.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))

		c2, err := ListenPacket(tt.net, addr2)
		if err != nil {
			t.Fatalf("ListenPacket failed: %v", err)
		}
		defer closer(c2, netstr[0], addr1, addr2)
		c2.LocalAddr()
		c2.SetDeadline(time.Now().Add(100 * time.Millisecond))
		c2.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
		c2.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))

		if _, err := c1.WriteTo(wb, c2.LocalAddr()); err != nil {
			t.Fatalf("PacketConn.WriteTo failed: %v", err)
		}
		rb2 := make([]byte, 128)
		if _, _, err := c2.ReadFrom(rb2); err != nil {
			t.Fatalf("PacketConn.ReadFrom failed: %v", err)
		}
		if _, err := c2.WriteTo(wb, c1.LocalAddr()); err != nil {
			t.Fatalf("PacketConn.WriteTo failed: %v", err)
		}
		rb1 := make([]byte, 128)
		if _, _, err := c1.ReadFrom(rb1); err != nil {
			t.Fatalf("PacketConn.ReadFrom failed: %v", err)
		}
	}
}

func TestConnAndPacketConn(t *testing.T) {
	closer := func(c PacketConn, net, addr1, addr2 string) {
		c.Close()
		switch net {
		case "unixgram":
			os.Remove(addr1)
			os.Remove(addr2)
		}
	}

	for i, tt := range packetConnTests {
		var wb []byte
		netstr := strings.Split(tt.net, ":")
		wb, skipOrFatalFn := packetConnTestData(t, netstr[0], i)
		if skipOrFatalFn != nil {
			skipOrFatalFn()
			continue
		}

		addr1, addr2 := tt.addr1(), tt.addr2()
		c1, err := ListenPacket(tt.net, addr1)
		if err != nil {
			t.Fatalf("ListenPacket failed: %v", err)
		}
		defer closer(c1, netstr[0], addr1, addr2)
		c1.LocalAddr()
		c1.SetDeadline(time.Now().Add(100 * time.Millisecond))
		c1.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
		c1.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))

		c2, err := Dial(tt.net, c1.LocalAddr().String())
		if err != nil {
			t.Fatalf("Dial failed: %v", err)
		}
		defer c2.Close()
		c2.LocalAddr()
		c2.RemoteAddr()
		c2.SetDeadline(time.Now().Add(100 * time.Millisecond))
		c2.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
		c2.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))

		if _, err := c2.Write(wb); err != nil {
			t.Fatalf("Conn.Write failed: %v", err)
		}
		rb1 := make([]byte, 128)
		if _, _, err := c1.ReadFrom(rb1); err != nil {
			t.Fatalf("PacetConn.ReadFrom failed: %v", err)
		}
		var dst Addr
		switch netstr[0] {
		case "ip":
			dst = &IPAddr{IP: IPv4(127, 0, 0, 1)}
		case "unixgram":
			continue
		default:
			dst = c2.LocalAddr()
		}
		if _, err := c1.WriteTo(wb, dst); err != nil {
			t.Fatalf("PacketConn.WriteTo failed: %v", err)
		}
		rb2 := make([]byte, 128)
		if _, err := c2.Read(rb2); err != nil {
			t.Fatalf("Conn.Read failed: %v", err)
		}
	}
}
<|endoftext|>


<commit_before>package main

import (
	. "github.com/xitongsys/parquet-go/ParquetHandler"
	. "github.com/xitongsys/parquet-go/ParquetType"
	"log"
	"os"
	"strconv"
	//"runtime/pprof"
)

type Student struct {
	Name   UTF8
	Age    INT32
	Id     INT64
	Weight DOUBLE
	Sex    BOOLEAN
	School UTF8
}

type MyFile struct {
	file *os.File
}

func (self *MyFile) Create(name string) error {
	file, err := os.Create(name)
	self.file = file
	return err
}
func (self *MyFile) Open(name string) error {
	file, err := os.Open(name)
	self.file = file
	return err
}
func (self *MyFile) Seek(offset int, pos int) (int64, error) {
	return self.file.Seek(int64(offset), pos)
}

func (self *MyFile) Read(b []byte) (n int, err error) {
	return self.file.Read(b)
}

func (self *MyFile) Write(b []byte) (n int, err error) {
	return self.file.Write(b)
}

func (self *MyFile) Close() {
	self.file.Close()
}

func main() {
	/*
		cpuf, _ := os.Create("cpu.profile")
		pprof.StartCPUProfile(cpuf)
		defer pprof.StopCPUProfile()
	*/

	fname := os.Args[1]
	num,_ := strconv.Atoi(os.Args[2])

	log.Println(fname, num)

	var f ParquetFile
	f = &MyFile{}

	//write flat
	f.Create(fname)
	ph := NewParquetHandler()
	ph.WriteInit(f, new(Student), 10, 30)

	for i := 0; i < num; i++ {
		stu := Student{
			Name:   UTF8("StudentName"),
			Age:    INT32(18 + i%10),
			Id:     INT64(i),
			Weight: DOUBLE(60 + i%10),
			Sex:    BOOLEAN(i%2 == 0),
			School: UTF8("PKU"),
		}
		ph.Write(stu)

		if i%(num/100) == 0 {
			log.Println(i*100/num, "%")
		}
	}
	ph.WriteStop()
	log.Println("Write Finished")
	f.Close()
	/*
		memf, _ := os.Create("mem.profile")
		pprof.WriteHeapProfile(memf)
		memf.Close()
	*/

}
<commit_msg>delete some files<commit_after><|endoftext|>