Skip to content

Commit

Permalink
rebase update
Browse files Browse the repository at this point in the history
  • Loading branch information
andeya committed Oct 11, 2017
1 parent 4276b38 commit e33aec2
Show file tree
Hide file tree
Showing 17 changed files with 1,375 additions and 1,240 deletions.
Empty file modified .gitignore 100755 → 100644
Empty file.
Empty file modified LICENSE 100755 → 100644
Empty file.
Empty file modified README.md 100755 → 100644
Empty file.
Empty file modified README_ZH.md 100755 → 100644
Empty file.
669 changes: 334 additions & 335 deletions agent.go 100755 → 100644

Large diffs are not rendered by default.

52 changes: 26 additions & 26 deletions agent_bsd.go 100755 → 100644
@@ -1,26 +1,26 @@
// +build darwin dragonfly freebsd netbsd openbsd

package surfer

import (
"runtime"
"syscall"
)

// osName returns the name of the OS.
func osName() string {
name, err := syscall.Sysctl("kern.ostype")
if err != nil {
return runtime.GOOS
}
return name
}

// osVersion returns the OS version.
func osVersion() string {
release, err := syscall.Sysctl("kern.osrelease")
if err != nil {
return "0.0"
}
return release
}
// +build darwin dragonfly freebsd netbsd openbsd

package surfer

import (
"runtime"
"syscall"
)

// osName returns the name of the OS.
func osName() string {
name, err := syscall.Sysctl("kern.ostype")
if err != nil {
return runtime.GOOS
}
return name
}

// osVersion returns the OS version.
func osVersion() string {
release, err := syscall.Sysctl("kern.osrelease")
if err != nil {
return "0.0"
}
return release
}
82 changes: 41 additions & 41 deletions agent_linux.go 100755 → 100644
@@ -1,41 +1,41 @@
// +build linux

package surfer

import (
"runtime"
"syscall"
)

// osName returns the name of the OS.
func osName() string {
buf := &syscall.Utsname{}
err := syscall.Uname(buf)
if err != nil {
return runtime.GOOS
}
return charsToString(buf.Sysname)
}

// osVersion returns the OS version.
func osVersion() string {
buf := &syscall.Utsname{}
err := syscall.Uname(buf)
if err != nil {
return "0.0"
}
return charsToString(buf.Release)
}

// charsToString converts a [65]int8 byte array into a string.
func charsToString(ca [65]int8) string {
s := make([]byte, len(ca))
var lens int
for ; lens < len(ca); lens++ {
if ca[lens] == 0 {
break
}
s[lens] = uint8(ca[lens])
}
return string(s[0:lens])
}
// +build linux,!arm

package surfer

import (
"runtime"
"syscall"
)

// osName returns the name of the OS.
func osName() string {
buf := &syscall.Utsname{}
err := syscall.Uname(buf)
if err != nil {
return runtime.GOOS
}
return charsToString(buf.Sysname)
}

// osVersion returns the OS version.
func osVersion() string {
buf := &syscall.Utsname{}
err := syscall.Uname(buf)
if err != nil {
return "0.0"
}
return charsToString(buf.Release)
}

// charsToString converts a [65]int8 byte array into a string.
func charsToString(ca [65]int8) string {
s := make([]byte, len(ca))
var lens int
for ; lens < len(ca); lens++ {
if ca[lens] == 0 {
break
}
s[lens] = uint8(ca[lens])
}
return string(s[0:lens])
}
41 changes: 41 additions & 0 deletions agent_linux_arm.go
@@ -0,0 +1,41 @@
// +build linux,arm

package surfer

import (
"runtime"
"syscall"
)

// osName returns the name of the OS.
func osName() string {
buf := &syscall.Utsname{}
err := syscall.Uname(buf)
if err != nil {
return runtime.GOOS
}
return charsToString(buf.Sysname)
}

// osVersion returns the OS version.
func osVersion() string {
buf := &syscall.Utsname{}
err := syscall.Uname(buf)
if err != nil {
return "0.0"
}
return charsToString(buf.Release)
}

// charsToString converts a [65]uint8 byte array into a string.
func charsToString(ca [65]uint8) string {
s := make([]byte, len(ca))
var lens int
for ; lens < len(ca); lens++ {
if ca[lens] == 0 {
break
}
s[lens] = uint8(ca[lens])
}
return string(s[0:lens])
}
50 changes: 25 additions & 25 deletions agent_windows.go 100755 → 100644
@@ -1,25 +1,25 @@
// +build windows

package surfer

import (
"fmt"
"runtime"
"syscall"
)

// osName returns the name of the OS.
func osName() string {
return runtime.GOOS
}

// osVersion returns the OS version.
func osVersion() string {
v, err := syscall.GetVersion()
if err != nil {
return "0.0"
}
major := uint8(v)
minor := uint8(v >> 8)
return fmt.Sprintf("%d.%d", major, minor)
}
// +build windows

package surfer

import (
"fmt"
"runtime"
"syscall"
)

// osName returns the name of the OS.
func osName() string {
return runtime.GOOS
}

// osVersion returns the OS version.
func osVersion() string {
v, err := syscall.GetVersion()
if err != nil {
return "0.0"
}
major := uint8(v)
minor := uint8(v >> 8)
return fmt.Sprintf("%d.%d", major, minor)
}
148 changes: 148 additions & 0 deletions body.go
@@ -0,0 +1,148 @@
// Copyright 2015 henrylee2cn Author. All Rights Reserved.
//
// 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 surfer

import (
"bytes"
"encoding/json"
"encoding/xml"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/url"
"strings"
)

// body set request body
type body interface {
SetBody(*Request) error
}

// Content bytes type of body content
type Content struct {
ContentType string
Bytes []byte
}

var _ body = new(Content)

// SetBody sets request body
func (c *Content) SetBody(r *Request) error {
r.Header.Set("Content-Type", c.ContentType)
r.body = bytes.NewReader(c.Bytes)
return nil
}

// Bytes bytes type of body content, without content type
type Bytes []byte

var _ body = Bytes("")

// SetBody sets request body
func (b Bytes) SetBody(r *Request) error {
r.body = bytes.NewReader(b)
return nil
}

type (
// Form impletes body interface
Form struct {
// Values [field name]-[]value
Values map[string][]string
// Files [field name]-[]File
Files map[string][]File
}
// File post form file
File struct {
Filename string
Bytes []byte
}
)

var _ body = new(Form)

// SetBody sets request body
func (f Form) SetBody(r *Request) error {
if len(f.Files) > 0 {
pr, pw := io.Pipe()
bodyWriter := multipart.NewWriter(pw)
go func() {
for fieldname, postfiles := range f.Files {
for _, postfile := range postfiles {
fileWriter, err := bodyWriter.CreateFormFile(fieldname, postfile.Filename)
if err != nil {
log.Println("[E] Surfer: Multipart:", err)
}
_, err = fileWriter.Write(postfile.Bytes)
if err != nil {
log.Println("[E] Surfer: Multipart:", err)
}
}
}
for k, v := range f.Values {
for _, vv := range v {
bodyWriter.WriteField(k, vv)
}
}
bodyWriter.Close()
pw.Close()
}()
r.Header.Set("Content-Type", bodyWriter.FormDataContentType())
r.body = ioutil.NopCloser(pr)
} else {
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
r.body = strings.NewReader(url.Values(f.Values).Encode())
}
return nil
}

// JSONObj JSON type of body content
type JSONObj struct{ Data interface{} }

var _ body = new(JSONObj)

// SetBody sets request body
func (obj *JSONObj) SetBody(r *Request) error {
r.Header.Set("Content-Type", "application/json;charset=utf-8")
if obj == nil || obj.Data == nil {
return nil
}
b, err := json.Marshal(obj.Data)
if err != nil {
return err
}
r.body = bytes.NewReader(b)
return nil
}

// XMLObj XML type of body content
type XMLObj struct{ Data interface{} }

var _ body = new(XMLObj)

// SetBody sets request body
func (obj *XMLObj) SetBody(r *Request) error {
r.Header.Set("Content-Type", "application/xml;charset=utf-8")
if obj == nil || obj.Data == nil {
return nil
}
b, err := xml.Marshal(obj.Data)
if err != nil {
return err
}
r.body = bytes.NewReader(b)
return nil
}

0 comments on commit e33aec2

Please sign in to comment.