Sfoglia il codice sorgente

add:demo http2改为http

Dozingfiretruck 3 anni fa
parent
commit
95b049b8a4

+ 9 - 9
components/network/libhttp/luat_lib_http.c

@@ -573,8 +573,8 @@ static int http_set_url(luat_http_ctrl_t *http_ctrl) {
 }
 
 /*
-http2客户端
-@api http2.request(method,url,headers,body,opts,ca_file)
+http客户端
+@api http.request(method,url,headers,body,opts,ca_file)
 @string 请求方法, 支持 GET/POST
 @string url地址
 @tabal  请求头 可选 例如{["Content-Type"] = "application/x-www-form-urlencoded"}
@@ -586,15 +586,15 @@ http2客户端
 @return string body
 @usage
 -- GET请求
-local code, headers, body = http2.request("GET","http://site0.cn/api/httptest/simple/time").wait()
-log.info("http2.get", code, headers, body)
+local code, headers, body = http.request("GET","http://site0.cn/api/httptest/simple/time").wait()
+log.info("http.get", code, headers, body)
 -- POST请求
-local code, headers, body = http2.request("POST","http://httpbin.com/post", {}, "abc=123").wait()
-log.info("http2.post", code, headers, body)
+local code, headers, body = http.request("POST","http://httpbin.com/post", {}, "abc=123").wait()
+log.info("http.post", code, headers, body)
 
 -- GET请求,但下载到文件
-local code, headers, body = http2.request("GET","http://httpbin.com/", {}, "", {dst="/data.bin"}).wait()
-log.info("http2.get", code, headers, body)
+local code, headers, body = http.request("GET","http://httpbin.com/", {}, "", {dst="/data.bin"}).wait()
+log.info("http.get", code, headers, body)
 */
 static int l_http_request(lua_State *L) {
 	size_t client_cert_len, client_key_len, client_password_len,len;
@@ -778,7 +778,7 @@ LUAMOD_API int luaopen_http( lua_State *L ) {
     return 1;
 }
 
-LUAMOD_API int luaopen_http2( lua_State *L ) {
+LUAMOD_API int luaopen_http( lua_State *L ) {
     lua_getglobal(L, "http");
     if (lua_isuserdata(L, -1))
         return 1;

+ 7 - 11
demo/socket/Air105/http_demo.lua

@@ -1,35 +1,31 @@
 
---[[
-这个demo未完成, 暂不可用
-TODO http2库完成后删除这段注释
-]]
 local function testTask()
 
     -- GET request, task内的同步操作
-    local code, headers, body = http2.request("GET","http://site0.cn/api/httptest/simple/time").wait()
-    log.info("http2.get", code, headers, body)
+    local code, headers, body = http.request("GET","http://site0.cn/api/httptest/simple/time").wait()
+    log.info("http.get", code, headers, body)
 
     -- POST request
     local req_headers = {}
     req_headers["Content-Type"] = "application/json"
     local body = json.encode({name="LuatOS"})
-    local code, headers, body = http2.request("POST","http://site0.cn/api/httptest/simple/date", 
+    local code, headers, body = http.request("POST","http://site0.cn/api/httptest/simple/date", 
             req_headers,
             body -- POST请求所需要的body, string, zbuff, file均可
     ).wait()
-    log.info("http2.post", code, headers, body)
+    log.info("http.post", code, headers, body)
 
     -- POST and download, task内的同步操作
     local opts = {}                 -- 额外的配置项
     opts["dst"] = "/data.bin"       -- 下载路径,可选
     opts["timeout"] = 30            -- 超时时长,单位秒,可选
     opts["adapter"] = socket.ETH0  -- 使用哪个网卡,可选
-    local code, headers, body = http2.request("POST","http://site0.cn/api/httptest/simple/date", 
+    local code, headers, body = http.request("POST","http://site0.cn/api/httptest/simple/date", 
             {}, -- 请求所添加的 headers, 可以是nil
             "", 
             opts
     ).wait()
-    log.info("http2.post", code, headers, body) -- 只返回code和headers
+    log.info("http.post", code, headers, body) -- 只返回code和headers
 
     local f = io.open("/data.bin", "rb")
     if f then
@@ -38,7 +34,7 @@ local function testTask()
     end
     
     -- GET request, 开个task让它自行执行去吧, 不管执行结果了
-    sys.taskInit(http2.request("GET","http://site0.cn/api/httptest/simple/time").wait)
+    sys.taskInit(http.request("GET","http://site0.cn/api/httptest/simple/time").wait)
 end
 
 function httpDemo()

+ 14 - 14
demo/socket/EC618/http_demo.lua

@@ -2,7 +2,7 @@ local libnet = require "libnet"
 
 
 -- local function downloadFile(code, headers, body)
---     log.info("http2.get", code, json.encode(headers), body)
+--     log.info("http.get", code, json.encode(headers), body)
 --     local r = io.writeFile("/data.txt", body)
 --     if r then
 --         log.info("文件写入成功")
@@ -18,12 +18,12 @@ sys.taskInit(function ()
     log.info("------下载文件------")
     local path = "/data.txt"
     -- GET请求,但下载到文件
-    http2.request(
+    http.request(
         "GET",
         "http://cdn.openluat-luatcommunity.openluat.com/attachment/20220825134126812_text.txt").cb
         (
             function (code, headers, body)
-                log.info("http2.get", code, json.encode(headers), body)
+                log.info("http.get", code, json.encode(headers), body)
                 local r = io.writeFile("/data.txt", body)
                 if r then
                     log.info("文件写入成功")
@@ -48,12 +48,12 @@ sys.taskInit(function ()
     -- opts["adapter"] = ""  -- 使用哪个网卡,可选
     -- local req_headers = {}
     -- req_headers["Content-Type"] = "application/json"
-    -- local code, headers, body = http2.request("POST","http://site0.cn/api/httptest/simple/date", 
+    -- local code, headers, body = http.request("POST","http://site0.cn/api/httptest/simple/date", 
     --         json.encode(req_headers), -- 请求所添加的 headers, 可以是nil
     --         "",
     --         opts
     -- ).wait()
-    -- log.info("http2.post", code, headers, body) -- 只返回code和headers
+    -- log.info("http.post", code, headers, body) -- 只返回code和headers
 end)
 
 -- [[
@@ -66,7 +66,7 @@ sys.taskInit(
 
         -- GET请求
         log.info("------GET请求------")
-        local code, headers, body = http2.request("GET","https://www.baidu.com/").wait()
+        local code, headers, body = http.request("GET","https://www.baidu.com/").wait()
         log.info("http.get", code, json.encode(headers), body)
         sys.wait(2000)
 
@@ -75,7 +75,7 @@ sys.taskInit(
         local req_headers = {}
         req_headers["Content-Type"] = "application/json"
         local body = json.encode({name="LuatOS"})
-        http2.request("POST","http://site0.cn/api/httptest/simple/date", 
+        http.request("POST","http://site0.cn/api/httptest/simple/date", 
             req_headers,
             body -- POST请求所需要的body, string, zbuff, file均可
         ).cb(function(code, headers, body)
@@ -94,24 +94,24 @@ sys.taskInit(
 --         -- while 1 do
 --             log.info("mem.lua", rtos.meminfo())
 --             log.info("mem.sys", rtos.meminfo("sys"))
---             local code, headers, body = http2.request("GET","https://www.baidu.com/").wait()
---             log.info("http2.get", code, json.encode(headers), body)
+--             local code, headers, body = http.request("GET","https://www.baidu.com/").wait()
+--             log.info("http.get", code, json.encode(headers), body)
 
---             -- -- local code, headers, body = http2.request("GET","http://site0.cn/api/httptest/simple/time").wait()
---             -- -- log.info("http2.get", code, json.encode(headers), body)
+--             -- -- local code, headers, body = http.request("GET","http://site0.cn/api/httptest/simple/time").wait()
+--             -- -- log.info("http.get", code, json.encode(headers), body)
 --             -- sys.wait(2000)
 
 --             -- -- POST request
 --             -- local req_headers = {}
 --             -- req_headers["Content-Type"] = "application/json"
 --             -- local body = json.encode({name="LuatOS"})
---             -- local code, headers, body = http2.request("POST","http://site0.cn/api/httptest/simple/date", 
+--             -- local code, headers, body = http.request("POST","http://site0.cn/api/httptest/simple/date", 
 --             --         req_headers,
 --             --         body -- POST请求所需要的body, string, zbuff, file均可
 --             -- ).cb(function (code, headers, body)
---             --     log.info("http2.post", code, headers, body)
+--             --     log.info("http.post", code, headers, body)
 --             -- end)
---             -- log.info("http2.post1", code, headers, body)
+--             -- log.info("http.post1", code, headers, body)
 --         -- end
 --     end
 -- )

+ 1 - 1
demo/wlan/esp32c3/hello_world/main.lua

@@ -30,7 +30,7 @@ sys.taskInit(function()
         sys.wait(100)
         -- local url = "http://ip.nutz.cn/json"
         local url = "http://nutzam.com/1.txt"
-        local code, headers, body = http2.request("GET", url).wait()
+        local code, headers, body = http.request("GET", url).wait()
         log.info("http", code, json.encode(headers), #body)
     else
         print("wlan NOT ready!!!!")

+ 2 - 2
script/turnkey/EinkBook/Scripts/main.lua

@@ -150,7 +150,7 @@ function showBook(bookName, bookUrl, page)
     sys.taskInit(function()
         waitHttpTask = true
         for i = 1, 3 do
-            local code, headers, data = http2.request("GET",bookUrl .. "/" .. page).wait()
+            local code, headers, data = http.request("GET",bookUrl .. "/" .. page).wait()
             log.info("SHOWBOOK", code)
             if code ~= 200 then
                 log.error("SHOWBOOK", "获取图书内容失败 ", data)
@@ -317,7 +317,7 @@ sys.taskInit(function()
         end
     end
     for i = 1, 5 do
-        local code, headers, data = http2.request("GET", serverAdress .. "getBooks").wait()
+        local code, headers, data = http.request("GET", serverAdress .. "getBooks").wait()
         log.info("SHOWBOOK", code)
         if code ~= 200 then
             log.error(tag, "获取图书列表失败 ", data)

+ 1 - 1
script/turnkey/eink-calendar/main.lua

@@ -30,7 +30,7 @@ local function connectWifi()
 end
 
 local function requestHttp()
-    local code, headers, body = http2.request("GET","http://apicn.luatos.org:23328/luatos-calendar/v1?mac=111&battery=10&location="..location.."&appid="..appid.."&appsecret="..appsecret).wait()
+    local code, headers, body = http.request("GET","http://apicn.luatos.org:23328/luatos-calendar/v1?mac=111&battery=10&location="..location.."&appid="..appid.."&appsecret="..appsecret).wait()
     if code == 200 then
         return body
     else