js API: http.get, http.loadScript

This commit is contained in:
zelig 2015-05-28 08:22:18 +01:00
parent a83e02416a
commit 60735bd70c
4 changed files with 86 additions and 13 deletions

View file

@ -88,6 +88,12 @@ func (js *jsre) adminBindings() {
debug.Set("seedhash", js.seedHash)
// undocumented temporary
debug.Set("waitForBlocks", js.waitForBlocks)
js.re.Set("http", struct{}{})
t, _ = js.re.Get("http")
http := t.Object()
http.Set("get", js.httpGet)
http.Set("loadScript", js.httpLoadScript)
}
// generic helper to getBlock by Number/Height or Hex depending on autodetected input
@ -203,6 +209,56 @@ func (js *jsre) resend(call otto.FunctionCall) otto.Value {
return otto.FalseValue()
}
func (js *jsre) httpLoadScript(call otto.FunctionCall) otto.Value {
if len(call.ArgumentList) != 1 {
fmt.Println("requires 1 argument: http.httpLoadScript(url)")
return otto.FalseValue()
}
uri, err := call.Argument(0).ToString()
if err != nil {
fmt.Println(err)
return otto.FalseValue()
}
script, err := ds.Get(uri, "")
if err != nil {
fmt.Println(err)
return otto.FalseValue()
}
_, err = call.Otto.Run(script)
if err != nil {
fmt.Println(err)
return otto.FalseValue()
}
return otto.TrueValue()
}
func (js *jsre) httpGet(call otto.FunctionCall) otto.Value {
if len(call.ArgumentList) > 2 {
fmt.Println("requires 1 or 2 arguments: http.get(url[, filepath])")
return otto.UndefinedValue()
}
uri, err := call.Argument(0).ToString()
if err != nil {
fmt.Println(err)
return otto.UndefinedValue()
}
var path string
if len(call.ArgumentList) > 1 {
path, err = call.Argument(1).ToString()
if err != nil {
fmt.Println(err)
return otto.UndefinedValue()
}
}
resp, err := ds.Get(uri, path)
if err != nil {
fmt.Println(err)
return otto.UndefinedValue()
}
v, _ := call.Otto.ToValue(string(resp))
return v
}
func (js *jsre) sign(call otto.FunctionCall) otto.Value {
if len(call.ArgumentList) != 2 {
fmt.Println("requires 2 arguments: eth.sign(signer, data)")

View file

@ -23,7 +23,7 @@ func (self *DirectoryString) String() string {
}
func (self *DirectoryString) Set(value string) error {
self.Value = expandPath(value)
self.Value = ExpandPath(value)
return nil
}
@ -119,7 +119,7 @@ func (self *DirectoryFlag) Set(value string) {
// 2. expands embedded environment variables
// 3. cleans the path, e.g. /a/b/../c -> /a/c
// Note, it has limitations, e.g. ~someuser/tmp will not be expanded
func expandPath(p string) string {
func ExpandPath(p string) string {
if strings.HasPrefix(p, "~/") || strings.HasPrefix(p, "~\\") {
if user, err := user.Current(); err == nil {
if err == nil {

View file

@ -20,7 +20,7 @@ func TestPathExpansion(t *testing.T) {
os.Setenv("DDDXXX", "/tmp")
for test, expected := range tests {
got := expandPath(test)
got := ExpandPath(test)
if got != expected {
t.Errorf("test %s, got %s, expected %s\n", test, got, expected)
}

View file

@ -5,6 +5,7 @@ import (
"io/ioutil"
"net/http"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
@ -35,16 +36,8 @@ func (self *DocServer) Client() *http.Client {
func (self *DocServer) GetAuthContent(uri string, hash common.Hash) (content []byte, err error) {
// retrieve content
resp, err := self.Client().Get(uri)
defer func() {
if resp != nil {
resp.Body.Close()
}
}()
if err != nil {
return
}
content, err = ioutil.ReadAll(resp.Body)
content, err = self.Get(uri, "")
if err != nil {
return
}
@ -61,3 +54,27 @@ func (self *DocServer) GetAuthContent(uri string, hash common.Hash) (content []b
return
}
func (self *DocServer) Get(uri, path string) (content []byte, err error) {
// retrieve content
resp, err := self.Client().Get(uri)
defer func() {
if resp != nil {
resp.Body.Close()
}
}()
if err != nil {
return
}
content, err = ioutil.ReadAll(resp.Body)
if err != nil {
return
}
if path != "" {
ioutil.WriteFile(utils.ExpandPath(path), content, 0700)
}
return
}