20190508_jeffrey - silenceuncrio/diary GitHub Wiki

0900

review

1000

開始來實作 session 的部分

1540

M300 上 HTTP 不同的 query 有不同的權限需求

可以參考原本 iweb 上的 _register_http_endpoint()

static void _register_http_endpoint(struct mg_connection *nc)
{
    mg_register_http_endpoint(nc, "/api/login", _api_login);
    mg_register_http_endpoint(nc, "/api/logout", _api_logout);
    mg_register_http_endpoint(nc, "/api/whoAmI", _api_whoAmI);

    mg_register_http_endpoint(nc, "/api/firmwareUpload", _api_firmwareUpload);

    mg_register_http_endpoint(nc, "/api/configurationUpload", _api_configurationUpload);
    mg_register_http_endpoint(nc, "/api/openvpnUpload", _api_openvpnUpload);
    mg_register_http_endpoint(nc, "/api/ipsecUpload", _api_ipsecUpload);
    mg_register_http_endpoint(nc, "/api/brandTopPngUpload", _api_brandTopPngUpload);
    mg_register_http_endpoint(nc, "/api/customerFileUpload", _api_customerFileUpload);

#if !defined(PROSRC_NO_DIDO) && defined(PROSRC_DO_REMOTE)
    mg_register_http_endpoint(nc, "/DO_ON",             _api_do_on);
    mg_register_http_endpoint(nc, "/DO_OFF",            _api_do_off);
    mg_register_http_endpoint(nc, "/DO_PULSE",          _api_do_pulse);
    mg_register_http_endpoint(nc, "/RESTORE_DO_ALARM",  _api_restore_do_alarm);
#endif

    // System / Time And Date
    mg_register_http_endpoint(nc, "/cgi-bin/sntp.cgi?act=config",    _handler_level_normal);
    mg_register_http_endpoint(nc, "/cgi-bin/sntp.cgi?act=apply",     _handler_level_admin);

    // System / COM Ports
    mg_register_http_endpoint(nc, "/cgi-bin/com.cgi?act=config",    _handler_level_normal);
    mg_register_http_endpoint(nc, "/cgi-bin/com.cgi?act=apply",     _handler_level_admin);
    mg_register_http_endpoint(nc, "/cgi-bin/vcom.cgi?act=config",   _handler_level_normal);
    mg_register_http_endpoint(nc, "/cgi-bin/vcom.cgi?act=apply",    _handler_level_admin);

    ...

先專注在 System / Time And Date 的部分

    // System / Time And Date
    mg_register_http_endpoint(nc, "/cgi-bin/sntp.cgi?act=config",    _handler_level_normal);
    mg_register_http_endpoint(nc, "/cgi-bin/sntp.cgi?act=apply",     _handler_level_admin);

之前是利用 mongoose 提供的 mg_register_http_endpoint() 把某個 uri_path 註冊到某個 handler

由於目前 iweb.fcgi 牽涉到 lighttpd 提供的 URL rewrite

以 sntp.cgi 為例

經過以下 lighttpd config 裡的 url rewrite 設定後

#### url handling modules (rewrite, redirect, access)
url.rewrite-once = (
  "^/api/whoAmI$" => "/fcgi-bin/iweb.fcgi?app=api&act=whoAmI",
  "^/cgi-bin/([a-zA-Z]+).cgi$" => "/fcgi-bin/iweb.fcgi?app=$1",
  "^/cgi-bin/([a-zA-Z]+).cgi\?act=([a-zA-Z]+)$" => "/fcgi-bin/iweb.fcgi?app=$1&act=$2"
)

/cgi-bin/sntp.cgi?act=config 會轉成 /fcgi-bin/iweb.fcgi?app=sntp&act=config

這個轉換是為了相容於之前的 html 部分的 api

不過我們當然可以使用正確的 fastcgi url - /fcgi-bin/iweb.fcgi?app=sntp&act=config

我們來看一下這兩個 uri 產生的相關 HTTP env

  • /fcgi-bin/iweb.fcgi?app=sntp&act=config
    • QUERY_STRING: "app=sntp&act=config"
    • REQUEST_URI: "/fcgi-bin/iweb.fcgi?app=sntp&act=config"
    • SCRIPT_FILENAME: "/www/fcgi-bin/iweb.fcgi"
    • SCRIPT_NAME: "/fcgi-bin/iweb.fcgi"
  • /cgi-bin/sntp.cgi?act=config
    • QUERY_STRING: "app=sntp&act=config"
    • REQUEST_URI: "/cgi-bin/sntp.cgi?act=config"
    • SCRIPT_FILENAME: "/www/fcgi-bin/iweb.fcgi"
    • SCRIPT_NAME: "/fcgi-bin/iweb.fcgi"

我們若要在 iweb.fcgi 的程式裡做像 _register_http_endpoint() 的事情

像 sntp.cgi 我們就不應該用 /cgi-bin/sntp.cgi?act=config 來做註冊的動作

不然不須 url rewrite 的 /fcgi-bin/iweb.fcgi?app=sntp&act=config 一進來就會找不到對應的 handler 了

觀察 /fcgi-bin/iweb.fcgi?app=sntp&act=config/cgi-bin/sntp.cgi?act=config 相關的 env 可以發現

兩者的 QUERY_STRING 都是 app=sntp&act=config

所以我們應該使用 app=sntp&act=config 來註冊相對應的 handler

這樣一來不管是 /fcgi-bin/iweb.fcgi?app=sntp&act=config/cgi-bin/sntp.cgi?act=config 就都可以對應到相同的 handler 了

不過更好的處理方式是寫一個專門處理權限的程式

權限過了就呼叫該 rule 提供的 handler

這樣個別的 CGI 就不用去處理 session 以及 權限 的細節

這需要思考一下

也許也可以先寫醜醜的先求有

再來 refactoring

試著把今天作的整理一下做個 commit

1750

commit 9a87b00d8f8d08fd1814efd2c8319d519672e412
Refs: [feature/lighttpd]
Author: jeffrey <[email protected]>
Date:   Wed May 8 17:49:57 2019 +0800

    porting /api/ahoAmI
    - cgi - /api/whoAmI?uuid=xxx
    - fcgi = /fcgi-bin/iweb.fcgi?app=api&act=whoAmI

    use cgi_init_values() to get cookie into jweb.in.session;
    then whoAmI() can according to this value to responce as previous iweb

 .../home/factory/icos/lighttpd/lighttpd.conf       |  1 +
 proscend/prosrc/fcgi/Makefile                      |  2 +-
 proscend/prosrc/fcgi/api.c                         | 50 ++++++++++++++++++++++
 proscend/prosrc/fcgi/app_sntp.c                    |  2 +-
 proscend/prosrc/fcgi/iweb.c                        |  6 ++-
 proscend/prosrc/fcgi/jweb.c                        | 37 ++++++++++++++++
 proscend/prosrc/fcgi/jweb.h                        |  6 +++
 7 files changed, 101 insertions(+), 3 deletions(-)