Skip to content

Commit

Permalink
Convert GET to POST request
Browse files Browse the repository at this point in the history
  • Loading branch information
Neph-Oo committed Mar 7, 2021
1 parent 6abf4e3 commit e48ba2a
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 33 deletions.
20 changes: 18 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@

LUA_VER=lua5.3
LUA_LIBS=`pkg-config --libs $(LUA_VER)`
LUA_INCL=`pkg-config --cflags $(LUA_VER)`

CURL=libcurl
CURL_LIBS=`pkg-config --libs $(CURL)`

MOD_INCL=include


RPATH=.:lib/:../lib
LFLAG=-Wl,-rpath="$(RPATH)"



All: modhttp

modhttp:
cd luaipfs && gcc -fPIC -shared -o http.so src/*.c -Iinclude `pkg-config --libs lua` `pkg-config --libs libcurl` -Wl,-rpath=".:lib/:../lib" -Wall
cd luaipfs && gcc -fPIC -shared -o http.so src/*.c -I$(MOD_INCL) $(LUA_LIBS) $(LUA_INCL) $(CURL_LIBS) $(LFLAG) -Wall

clean:
cd luaipfs && test -f http.so && rm -f http.so || test 0
cd luaipfs && rm -f src/*.o
cd luaipfs && rm -f src/*.o
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
![IPFS http logo](https://user-images.githubusercontent.com/1211152/29604883-ca3a4028-87e0-11e7-9f9a-75de49b06048.png)

# [WIP] Lua-ipfs
Ipfs API Lua module. Partialy implemented.
Lua Ipfs http client library, partialy implemented.

You need a running ipfs daemon. Currently, tested with the Go implementation of ipfs.
You need go-ipfs running in the background for using this module. Currently, it has only been tested with the Go implementation of ipfs.


## Install

```luarocks install luaipfs``` [wip]

Or compile it yourself. Libcurl (C library), luajson and luafilesystem modules are needed.
Or compile it yourself. You will need libcurl (C library), luajson and luafilesystem modules.



Expand All @@ -24,7 +24,7 @@ local luaipfs = require("luaipfs")
local ipfs = luaipfs:new()
```

And use it to download a file from ifps:
And download a file from ifps:
```
local file = io.open("dot.gif", "w+")
file:write(
Expand All @@ -33,8 +33,6 @@ file:write(
file:close()
```

Of course, you can do a lot more. Read the documentation.


## Documentation

Expand All @@ -43,7 +41,6 @@ See *doc/luaipfs.md* for a list of available functions.
Or ```luarocks doc luaipfs```.

Some useful informations can be found on the ipfs website.
The API reference is here: https://ipfs.io/docs/api/.
You can found official api reference here: https://ipfs.io/docs/api/.


De la doc en français est également disponible à cette adresse: https://wiki.gauf.re/p2p/lua-ipfs-api (wip)
12 changes: 6 additions & 6 deletions luaipfs/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function base:get (ipfs_path, out_path)
end


local res, http_ret, err = self.http:get("/api/v0/get?arg=" .. ipfs_path, out_path)
local res, http_ret, err = self.http:post("/api/v0/get?arg=" .. ipfs_path, out_path)
if not res then
if err and not is_json(err) then return false, err end
return false, err and (json.decode(err)).Message or http_ret
Expand All @@ -83,7 +83,7 @@ function base:adv_get (ipfs_path, cb_func)
return false, "Invalid parameter #1 (function expected, got " .. type(cb_func) .. ")"
end

local res, http_ret, err = self.http:get_cb("/api/v0/get?arg=" .. ipfs_path, ipfs_path, cb_func)
local res, http_ret, err = self.http:post_cb("/api/v0/get?arg=" .. ipfs_path, ipfs_path, cb_func)
if not res then
if err and not is_json(err) then return false, err end
return false, err and (json.decode(err)).Message or http_ret
Expand All @@ -105,7 +105,7 @@ function base:cat (ipfs_path, out_path)
end


local res, http_ret, err = self.http:get("/api/v0/cat?arg=" .. ipfs_path, out_path)
local res, http_ret, err = self.http:post("/api/v0/cat?arg=" .. ipfs_path, out_path)
if not res then
if err and not is_json(err) then return false, err end
return false, err and (json.decode(err)).Message or http_ret
Expand All @@ -123,7 +123,7 @@ function base:adv_cat (ipfs_path, cb_func)
return false, "Invalid parameter #1 (function expected, got " .. type(cb_func) .. ")"
end

local res, http_ret, err = self.http:get_cb("/api/v0/cat?arg=" .. ipfs_path, ipfs_path, cb_func)
local res, http_ret, err = self.http:post_cb("/api/v0/cat?arg=" .. ipfs_path, ipfs_path, cb_func)
if not res then
if err and not is_json(err) then return false, err end
return false, err and (json.decode(err)).Message or http_ret
Expand Down Expand Up @@ -205,7 +205,7 @@ function base:ls (ipfs_path)
return false, "Invalid parameter #1 (string expected, got " .. type(ipfs_path) .. ")"
end

local res, http_ret, err = self.http:get("/api/v0/ls?arg=" .. ipfs_path)
local res, http_ret, err = self.http:post("/api/v0/ls?arg=" .. ipfs_path)
if not res then
if err and not is_json(err) then return false, err end
return false, err and (json.decode(err)).Message or http_ret
Expand Down Expand Up @@ -235,7 +235,7 @@ function base:id (node_id)

local req = node_id and ("/api/v0/id?arg=" .. node_id) or "/api/v0/id"

local res, http_ret, err = self.http:get(req)
local res, http_ret, err = self.http:post(req)
if not res then
if err and not is_json(err) then return false, err end
return false, err and (json.decode(err)).Message or http_ret
Expand Down
2 changes: 1 addition & 1 deletion luaipfs/bitswap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function bitswap:bitswap_ledger (nodeid)
return false, "Invalid parameter #1 (string expected, got " .. type(nodeid) .. ")"
end

local res, http_ret, err = self.http:get("/api/v0/bitswap/ledger?arg=" .. nodeid)
local res, http_ret, err = self.http:post("/api/v0/bitswap/ledger?arg=" .. nodeid)
if not res then
if err and not is_json(err) then return false, err end
return false, err and (json.decode(err)).Message or http_ret
Expand Down
13 changes: 6 additions & 7 deletions luaipfs/dht.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ function dht:dht_findpeer (nodeid)
return false, "Invalid parameter #1 (string expected, got " .. type(nodeid) .. ")"
end

local res, http_ret, err = self.http:get("/api/v0/dht/findpeer?arg=" .. nodeid)
local res, http_ret, err = self.http:post("/api/v0/dht/findpeer?arg=" .. nodeid)
if not res then
if err and not is_json(err) then return false, err end
return false, err and (json.decode(err)).Message or http_ret
end


--Parse each line from answer

local t = {}
Expand Down Expand Up @@ -81,7 +80,7 @@ function dht:dht_findprovs (key)
return false, "Invalid parameter #1 (string expected, got " .. type(key) .. ")"
end

local res, http_ret, err = self.http:get("/api/v0/dht/findprovs?arg=" .. key)
local res, http_ret, err = self.http:post("/api/v0/dht/findprovs?arg=" .. key)
if not res then
if err and not is_json(err) then return false, err end
return false, err and (json.decode(err)).Message or http_ret
Expand Down Expand Up @@ -117,7 +116,7 @@ function dht:dht_get (key)
return false, "Invalid parameter #1 (string expected, got " .. type(key) .. ")"
end

local res, http_ret, err = self.http:get("/api/v0/dht/get?arg=" .. key)
local res, http_ret, err = self.http:post("/api/v0/dht/get?arg=" .. key)
if not res then
if err and not is_json(err) then return false, err end
return false, err and (json.decode(err)).Message or http_ret
Expand Down Expand Up @@ -155,7 +154,7 @@ function dht:dht_put (key, value)
return false, "Invalid parameter #2 (string expected, got " .. type(value) .. ")"
end

local res, http_ret, err = self.http:get("/api/v0/dht/put?arg=" .. key .. "&arg=" .. value)
local res, http_ret, err = self.http:post("/api/v0/dht/put?arg=" .. key .. "&arg=" .. value)
if not res then
if err and not is_json(err) then return false, err end
return false, err and (json.decode(err)).Message or http_ret
Expand Down Expand Up @@ -189,7 +188,7 @@ function dht:dht_query (node_id)
return false, "Invalid parameter #1 (string expected, got " .. type(node_id) .. ")"
end

local res, http_ret, err = self.http:get("/api/v0/dht/query?arg=" .. node_id)
local res, http_ret, err = self.http:post("/api/v0/dht/query?arg=" .. node_id)
if not res then
if err and not is_json(err) then return false, err end
return false, err and (json.decode(err)).Message or http_ret
Expand Down Expand Up @@ -222,7 +221,7 @@ function dht:dht_provide (key)
return false, "Invalid parameter #1 (string expected, got " .. type(key) .. ")"
end

local res, http_ret, err = self.http:get("/api/v0/dht/provide?arg=" .. key)
local res, http_ret, err = self.http:post("/api/v0/dht/provide?arg=" .. key)
if not res then
if err and not is_json(err) then return false, err end
return false, err and (json.decode(err)).Message or http_ret
Expand Down
2 changes: 1 addition & 1 deletion luaipfs/pin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function pin:pin_rm (ipfs_path)
return false, "Invalid parameter #1 (string expected, got " .. type(ipfs_path) .. ")"
end

local res, http_ret, err = self.http:get("/api/v0/pin/rm?arg=" .. ipfs_path)
local res, http_ret, err = self.http:post("/api/v0/pin/rm?arg=" .. ipfs_path)
if not res then
if err and not is_json(err) then return false, err end
return false, err and (json.decode(err)).Message or http_ret
Expand Down
21 changes: 14 additions & 7 deletions luaipfs/src/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@

static int ipfs_http_new (lua_State *);

static int ipfs_http_get (lua_State *);
static int ipfs_http_post (lua_State *);

static int ipfs_http_get_cb (lua_State *);
static int ipfs_http_post_cb (lua_State *);

static int ipfs_post_multipart_data (lua_State *);


static const struct luaL_Reg ipfs_http[] = {
{ "new", ipfs_http_new },
{ "get", ipfs_http_get },
{ "get_cb", ipfs_http_get_cb },
{ "post", ipfs_http_post },
{ "post_cb", ipfs_http_post_cb },
{ "post_multipart_data", ipfs_post_multipart_data },
{ NULL, NULL }
};
Expand Down Expand Up @@ -79,7 +79,7 @@ static int ipfs_http_new (lua_State *L) {



static int ipfs_http_get (lua_State *L) {
static int ipfs_http_post (lua_State *L) {
CURL *curl; CURLcode ret;
const char *url;
writeback_buffer buffer = {'\x00'};
Expand Down Expand Up @@ -152,6 +152,9 @@ static int ipfs_http_get (lua_State *L) {
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 50);
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, timeout);

curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0);

if (!fd)
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cb_write_to_buffer);
else
Expand All @@ -164,14 +167,14 @@ static int ipfs_http_get (lua_State *L) {




//Perform request, return answer||true||false and http response code + optional err

ret = curl_easy_perform(curl);
if (ret != CURLE_OK && ret != CURLE_OPERATION_TIMEDOUT)
module_error(L, (char *)curl_easy_strerror(ret));



if (buffer.base.hs.tot_items < 1 || buffer.base.error || ret == CURLE_OPERATION_TIMEDOUT) {
lua_pushboolean(L, 0);
} else {
Expand Down Expand Up @@ -230,7 +233,7 @@ static int ipfs_http_get (lua_State *L) {



static int ipfs_http_get_cb (lua_State *L) {
static int ipfs_http_post_cb (lua_State *L) {
CURL *curl; CURLcode ret;
int i; const char *url;
writeback_luacb cb = {'\x00'};
Expand Down Expand Up @@ -297,6 +300,10 @@ static int ipfs_http_get_cb (lua_State *L) {
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 50);
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, timeout);

curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0);


curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cb_write_to_luacb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &cb);
Expand Down
2 changes: 1 addition & 1 deletion luaipfs/swarm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function swarm:swarm_peers (prot, latency)
.. tostring(prot) .. "&latency=" .. tostring(latency)


local res, http_ret, err = self.http:get(api_call)
local res, http_ret, err = self.http:post(api_call)
if not res then
if err and not is_json(err) then return false, err end
return false, err and (json.decode(err)).Message or http_ret
Expand Down

0 comments on commit e48ba2a

Please sign in to comment.