<commit_before>/*
 * Minimal object storage library (C) 2015 Minio, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package objectstorage

import (
	"bytes"
	"crypto/hmac"
	"crypto/sha1"
	"encoding/base64"
	"fmt"
	"io"
	"net/http"
	"net/url"
	"sort"
	"strings"
	"time"
)

// operation - rest operation
type operation struct {
	HTTPServer string
	HTTPMethod string
	HTTPPath   string
}

// request - a http request
type request struct {
	req    *http.Request
	config *Config
}

// newRequest - instantiate a new request
func newRequest(op *operation, config *Config, body io.ReadCloser) (*request, error) {
	// if no method default to POST
	method := op.HTTPMethod
	if method == "" {
		method = "POST"
	}
	// parse URL for the combination of HTTPServer + HTTPPath
	u, err := url.Parse(op.HTTPServer + op.HTTPPath)
	if err != nil {
		return nil, err
	}
	// get a new HTTP request, for the requested method
	req, err := http.NewRequest(method, u.String(), nil)
	if err != nil {
		return nil, err
	}

	// add body
	req.Body = body
	// set UserAgent
	req.Header.Set("User-Agent", config.userAgent)

	// set ContentType, if available
	if config.ContentType != "" {
		req.Header.Set("Content-Type", config.ContentType)
	}
	// save for subsequent use
	r := new(request)
	r.config = config
	r.req = req
	return r, nil
}

// Do - start the request
func (r *request) Do() (resp *http.Response, err error) {
	r.SignV2()
	client := &http.Client{}
	return client.Do(r.req)
}

// Set - set additional headers if any
func (r *request) Set(key, value string) {
	r.req.Header.Set(key, value)
}

// Get - get header values
func (r *request) Get(key string) string {
	return r.req.Header.Get(key)
}

// SignV4 the request before Do() (version 4.0)
func (r *request) SignV4() {
	// TODO
}

// SignV2 the request before Do() (version 2.0)
func (r *request) SignV2() {
	// Add date if not present
	if date := r.Get("Date"); date == "" {
		r.Set("Date", time.Now().UTC().Format(http.TimeFormat))
	}

	// calculate HMAC for the secretaccesskey
	hm := hmac.New(sha1.New, []byte(r.config.SecretAccessKey))
	ss := r.mustGetStringToSign()
	hm.Write([]byte(ss))

	// prepare auth header
	authHeader := new(bytes.Buffer)
	fmt.Fprintf(authHeader, "AWS %s:", r.config.AccessKeyID)
	encoder := base64.NewEncoder(base64.StdEncoding, authHeader)
	encoder.Write(hm.Sum(nil))
	encoder.Close()

	// Set Authorization header
	r.Set("Authorization", authHeader.String())
}

// From the Amazon docs:
//
// StringToSign = HTTP-Verb + "\n" +
// 	 Content-MD5 + "\n" +
//	 Content-Type + "\n" +
//	 Date + "\n" +
//	 CanonicalizedAmzHeaders +
//	 CanonicalizedResource;
func (r *request) mustGetStringToSign() string {
	buf := new(bytes.Buffer)
	// write standard headers
	r.mustWriteDefaultHeaders(buf)
	// write canonicalized AMZ headers if any
	r.mustWriteCanonicalizedAmzHeaders(buf)
	// write canonicalized Query resources if any
	r.mustWriteCanonicalizedResource(buf)
	return buf.String()
}

func (r *request) mustWriteDefaultHeaders(buf *bytes.Buffer) {
	buf.WriteString(r.req.Method)
	buf.WriteByte('\n')
	buf.WriteString(r.req.Header.Get("Content-MD5"))
	buf.WriteByte('\n')
	buf.WriteString(r.req.Header.Get("Content-Type"))
	buf.WriteByte('\n')
	buf.WriteString(r.req.Header.Get("Date"))
	buf.WriteByte('\n')
}

func (r *request) mustWriteCanonicalizedAmzHeaders(buf *bytes.Buffer) {<commit_msg>Add proper date header and honor X-Amz-Date<commit_after>/*
 * Minimal object storage library (C) 2015 Minio, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package objectstorage

import (
	"bytes"
	"crypto/hmac"
	"crypto/sha1"
	"encoding/base64"
	"fmt"
	"io"
	"net/http"
	"net/url"
	"sort"
	"strings"
	"time"
)

// operation - rest operation
type operation struct {
	HTTPServer string
	HTTPMethod string
	HTTPPath   string
}

// request - a http request
type request struct {
	req    *http.Request
	config *Config
}

// newRequest - instantiate a new request
func newRequest(op *operation, config *Config, body io.ReadCloser) (*request, error) {
	// if no method default to POST
	method := op.HTTPMethod
	if method == "" {
		method = "POST"
	}
	// parse URL for the combination of HTTPServer + HTTPPath
	u, err := url.Parse(op.HTTPServer + op.HTTPPath)
	if err != nil {
		return nil, err
	}
	// get a new HTTP request, for the requested method
	req, err := http.NewRequest(method, u.String(), nil)
	if err != nil {
		return nil, err
	}

	// add body
	req.Body = body
	// set UserAgent
	req.Header.Set("User-Agent", config.userAgent)

	// set ContentType, if available
	if config.ContentType != "" {
		req.Header.Set("Content-Type", config.ContentType)
	}
	// save for subsequent use
	r := new(request)
	r.config = config
	r.req = req
	return r, nil
}

// Do - start the request
func (r *request) Do() (resp *http.Response, err error) {
	r.SignV2()
	client := &http.Client{}
	return client.Do(r.req)
}

// Set - set additional headers if any
func (r *request) Set(key, value string) {
	r.req.Header.Set(key, value)
}

// Get - get header values
func (r *request) Get(key string) string {
	return r.req.Header.Get(key)
}

// SignV4 the request before Do() (version 4.0)
func (r *request) SignV4() {
	// TODO
}

// SignV2 the request before Do() (version 2.0)
func (r *request) SignV2() {
	// Add date if not present
	if date := r.Get("Date"); date == "" {
		r.Set("X-Amz-Date", time.Now().UTC().Format(http.TimeFormat))
	}

	// calculate HMAC for the secretaccesskey
	hm := hmac.New(sha1.New, []byte(r.config.SecretAccessKey))
	ss := r.mustGetStringToSign()
	hm.Write([]byte(ss))

	// prepare auth header
	authHeader := new(bytes.Buffer)
	fmt.Fprintf(authHeader, "AWS %s:", r.config.AccessKeyID)
	encoder := base64.NewEncoder(base64.StdEncoding, authHeader)
	encoder.Write(hm.Sum(nil))
	encoder.Close()

	// Set Authorization header
	r.Set("Authorization", authHeader.String())
}

// From the Amazon docs:
//
// StringToSign = HTTP-Verb + "\n" +
// 	 Content-MD5 + "\n" +
//	 Content-Type + "\n" +
//	 Date + "\n" +
//	 CanonicalizedAmzHeaders +
//	 CanonicalizedResource;
func (r *request) mustGetStringToSign() string {
	buf := new(bytes.Buffer)
	// write standard headers
	r.mustWriteDefaultHeaders(buf)
	// write canonicalized AMZ headers if any
	r.mustWriteCanonicalizedAmzHeaders(buf)
	// write canonicalized Query resources if any
	r.mustWriteCanonicalizedResource(buf)
	return buf.String()
}

func (r *request) mustWriteDefaultHeaders(buf *bytes.Buffer) {
	buf.WriteString(r.req.Method)
	buf.WriteByte('\n')
	buf.WriteString(r.req.Header.Get("Content-MD5"))
	buf.WriteByte('\n')
	buf.WriteString(r.req.Header.Get("Content-Type"))
	buf.WriteByte('\n')
	if r.req.Header.Get("X-Amz-Date") == "" {
		buf.WriteString(r.req.Header.Get("Date"))
	}
	buf.WriteByte('\n')
}

func (r *request) mustWriteCanonicalizedAmzHeaders(buf *bytes.Buffer) {
{'fraction_non_alphanumeric': 0.14161886551943914, 'fraction_numerical': 0.006883365200764819, 'mean_word_length': 3.717979555021046, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1835269', 'n_tokens_mistral': 2698, 'n_tokens_neox': 2680, 'n_words': 776}