From 60735bd70c1837ca14561c9d0dea949f96dfdfd1 Mon Sep 17 00:00:00 2001 From: zelig Date: Thu, 28 May 2015 08:22:18 +0100 Subject: [PATCH] js API: http.get, http.loadScript --- cmd/geth/admin.go | 56 +++++++++++++++++++++++++++++++++++ cmd/utils/customflags.go | 4 +-- cmd/utils/customflags_test.go | 2 +- common/docserver/docserver.go | 37 ++++++++++++++++------- 4 files changed, 86 insertions(+), 13 deletions(-) diff --git a/cmd/geth/admin.go b/cmd/geth/admin.go index d398aa5f80..45decfa7f5 100644 --- a/cmd/geth/admin.go +++ b/cmd/geth/admin.go @@ -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)") diff --git a/cmd/utils/customflags.go b/cmd/utils/customflags.go index 78a6b8d220..2488314f79 100644 --- a/cmd/utils/customflags.go +++ b/cmd/utils/customflags.go @@ -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 { diff --git a/cmd/utils/customflags_test.go b/cmd/utils/customflags_test.go index 11deb38ef2..8093db5f92 100644 --- a/cmd/utils/customflags_test.go +++ b/cmd/utils/customflags_test.go @@ -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) } diff --git a/common/docserver/docserver.go b/common/docserver/docserver.go index c95197863c..41710fc961 100644 --- a/common/docserver/docserver.go +++ b/common/docserver/docserver.go @@ -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 + +}