lighttpd:ajaxリクエストは、実行するのではなくcgiスクリプトの内容を出力します。

lighttpd:ajaxリクエストは、実行するのではなくcgiスクリプトの内容を出力します。

ARM環境でlighttpdバージョン1.4.55を使用しています。いくつかのjsonデータをダウンロードするボタンを持つHTMLページを作成しました。このボタンは、CGI スクリプトを呼び出すフォームの送信をトリガーします。スクリプトはフォームの出力を取得してファイルに書き込む必要があります。ただし、ボタンをクリックすると、xhrリクエストの応答テキストはprintfメッセージではなくcgiスクリプトの内容です。 cgiには実行権限があります。

フォルダを次のように分割します。

•mnt/userfs/lighttpd/
    •www
        • /scripts_files
            json.cgi
        • /html_files
            •css folder
            •js folder
        • upload_dir
        • index.html
        • /admin
            • password_file
            • main.html
            • file_upload.html
        • /user
            • password_file
            • main.html
            • file_upload.html
    •lighttpd.conf
    •log
        •error.log

フォームを呼び出すボタンは次のとおりです。

<a href="/scripts_files/conf.json" download="data.json">            
    <input type="button" value="DOWNLOAD" onclick="submit_form();">
</a>

ボタンは関数を呼び出し、.cgiスクリプトを使用して生成されたファイルをダウンロードします。

Ajaxリクエスト機能:

function submit_form()
    {
        var div1 = document.getElementById("extern");
        var data = {};
        data = recursive_f(div1, 0, 0);
        output = JSON.stringify(data);
        var xhr_lv = new XMLHttpRequest();
        xhr_lv.onreadystatechange=function()
        xhr_lv.open("POST", "/scripts_files/json.cgi", true);
        xhr_lv.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
        xhr_lv.send(output);
    }

.cgiスクリプトを生成するCプログラム:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char* argv[])
{
    char* post_len_v = getenv("CONTENT_LENGTH");
    long post_len = strtol(post_len_v, NULL, 10);
    char* post_msg = (char*)malloc(post_len + 1);
    FILE *fp;


    if (!post_msg)
    { 
        return 0; 
    }
    fgets(post_msg, post_len + 1, stdin);      

    fp = fopen("/mnt/userfs/lighttpd/www/scripts_files/conf.json", "w");

    fprintf(fp, "%s", post_msg);
    fclose(fp);

    printf("Content-type: application/json\n\n");

    return 0;
}

Lighttpd 構成ファイル:

server.modules              = (
                "mod_indexfile",
                "mod_access",
                "mod_redirect",
                "mod_alias",
                "mod_compress",
                "mod_dirlisting",
                "mod_staticfile",
                "mod_auth",
                "mod_authn_file",
                "mod_accesslog",
                "mod_cgi",
                #"mod_rewrite",
                #"mod_status"
                #"mod_fastcgi"
)

server.document-root        = "/mnt/userfs/lighttpd/www"


server.errorlog             = "/mnt/userfs/lighttpd/log/error.log"
server.breakagelog         = "/mnt/userfs/lighttpd/log/breakage.log"

index-file.names            = ("index.html", "main.html", "file_upload.html")

mimetype.assign = (
    ".class" => "application/java-vm",
    ".js" => "application/javascript",
    ".mjs" => "application/javascript",
    ".json" => "application/json",
    ".jsonld" => "application/ld+json",
    ".wmx" => "video/x-ms-wmx",
    ".wvx" => "video/x-ms-wvx",
    ".avi" => "video/x-msvideo",
    ".movie" => "video/x-sgi-movie",
    ".ice" => "x-conference/x-cooltalk",
    ".sisx" => "x-epoc/x-sisx-app",
    ".vrm" => "x-world/x-vrml",
    "README" => "text/plain; charset=utf-8",
    "Makefile" => "text/x-makefile; charset=utf-8",
    
    # enable caching for unknown mime types:
    #"" => "application/octet-stream"
)

mimetype.use-xattr        = "disable" 

url.access-deny             = ( "~", ".inc" )

static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )

server.port                = 80

server.username            = "midac"
server.groupname           = "midac"

#compress.cache-dir          = "/var/cache/lighttpd/compress/"
compress.filetype           = ( "application/javascript", "text/css", "text/html", "text/plain" )

cgi.assign = ( ".cgi" => "" )

$HTTP["url"] =~ "/admin" {
auth.backend = "htpasswd"
auth.backend.htpasswd.userfile = "/mnt/userfs/lighttpd/www/admin/.htpasswd"
auth.require = ( "/admin" => (
    "method" => "basic", 
    "realm" => "main", 
    "require" => "valid-user") 
)
}

$HTTP["url"] =~ "/user" {
auth.backend = "htpasswd"
auth.backend.htpasswd.userfile = "/mnt/userfs/lighttpd/www/user/.htpasswd"
auth.require = ( "/user" => (
    "method" => "basic", 
    "realm" => "main", 
    "require" => "valid-user") 
)
}

$HTTP["url"] =~ "/user2" {
auth.backend = "htpasswd"
auth.backend.htpasswd.userfile = "/mnt/userfs/lighttpd/www/user2/.htpasswd"
auth.require = ( "/user2" => (
    "method" => "basic", 
    "realm" => "main", 
    "require" => "valid-user") 
)
}

また、サンプルcgiスクリプトを試しましたが、次のような結果が得られました。

#!/bin/sh

echo hello

だからCGIスクリプトの内容です。

POSTリクエストタイプはオクテットストリームです。 cgi_modが正しく動作しないか、lighttpd設定ファイルから何かを見逃したようです。

どんな提案がありますか?

ベストアンサー1

この質問は、他の場所に「どのくらい多いかわからない」に何度も投稿されました。回答者:https://serverfault.com/questions/1125701/lighttpd-ajax-request-prints-the-content-of-cgi-script-instead-of-running-it/1133316#1133316

おすすめ記事