mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-23 05:06:43 +00:00
js API: http.get, http.loadScript
This commit is contained in:
parent
a83e02416a
commit
60735bd70c
4 changed files with 86 additions and 13 deletions
|
|
@ -88,6 +88,12 @@ func (js *jsre) adminBindings() {
|
||||||
debug.Set("seedhash", js.seedHash)
|
debug.Set("seedhash", js.seedHash)
|
||||||
// undocumented temporary
|
// undocumented temporary
|
||||||
debug.Set("waitForBlocks", js.waitForBlocks)
|
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
|
// 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()
|
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 {
|
func (js *jsre) sign(call otto.FunctionCall) otto.Value {
|
||||||
if len(call.ArgumentList) != 2 {
|
if len(call.ArgumentList) != 2 {
|
||||||
fmt.Println("requires 2 arguments: eth.sign(signer, data)")
|
fmt.Println("requires 2 arguments: eth.sign(signer, data)")
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ func (self *DirectoryString) String() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *DirectoryString) Set(value string) error {
|
func (self *DirectoryString) Set(value string) error {
|
||||||
self.Value = expandPath(value)
|
self.Value = ExpandPath(value)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -119,7 +119,7 @@ func (self *DirectoryFlag) Set(value string) {
|
||||||
// 2. expands embedded environment variables
|
// 2. expands embedded environment variables
|
||||||
// 3. cleans the path, e.g. /a/b/../c -> /a/c
|
// 3. cleans the path, e.g. /a/b/../c -> /a/c
|
||||||
// Note, it has limitations, e.g. ~someuser/tmp will not be expanded
|
// 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 strings.HasPrefix(p, "~/") || strings.HasPrefix(p, "~\\") {
|
||||||
if user, err := user.Current(); err == nil {
|
if user, err := user.Current(); err == nil {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ func TestPathExpansion(t *testing.T) {
|
||||||
os.Setenv("DDDXXX", "/tmp")
|
os.Setenv("DDDXXX", "/tmp")
|
||||||
|
|
||||||
for test, expected := range tests {
|
for test, expected := range tests {
|
||||||
got := expandPath(test)
|
got := ExpandPath(test)
|
||||||
if got != expected {
|
if got != expected {
|
||||||
t.Errorf("test %s, got %s, expected %s\n", test, got, expected)
|
t.Errorf("test %s, got %s, expected %s\n", test, got, expected)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/cmd/utils"
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"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) {
|
func (self *DocServer) GetAuthContent(uri string, hash common.Hash) (content []byte, err error) {
|
||||||
// retrieve content
|
// retrieve content
|
||||||
resp, err := self.Client().Get(uri)
|
|
||||||
defer func() {
|
content, err = self.Get(uri, "")
|
||||||
if resp != nil {
|
|
||||||
resp.Body.Close()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
content, err = ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -61,3 +54,27 @@ func (self *DocServer) GetAuthContent(uri string, hash common.Hash) (content []b
|
||||||
return
|
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
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue