How caddy-cgi plugin execute a CGI script?

I want to know how caddy-cgi passes PWD to CGI scripts.
So I’m reading caddy-cgi source code ( GitHub - jung-kurt/caddy-cgi: Common Gateway Interface plugin for the Caddy HTTP server ), but I did not understand at last how this plugin fork a process.
I couldn’t find out any go code like “exec.Command()” in this repository.
Then, how it forks a CGI process?

I’m a novice for both golang and caddy.
Please help.

I want to know how caddy-cgi passes PWD to CGI scripts.

If by PWD you mean current working directory (as in the Unix command pwd), caddy-cgi does not pass that in the environment directly. However, the values associated with environment variable keys are all subject to placeholder substitution, just as with the script name and arguments, so you can pass the current working directory with the directive

env PWD={.}

If by PWD you mean password there are some different situations. If you use basic authentication, the CGI plugin will either not be run at all (if the authentication does not succeed) or it will run but not have access to the password (if authentication succeeds).

If you use your own username/password authentication in a form, the form values will be passed in the environment and you can retrieve them with the names of the form fields from the POST_DATA or QUERY_STRING environment variables. (For example, in PHP this might be $_POST['pwd'].)

So I’m reading caddy-cgi source code ( GitHub - jung-kurt/caddy-cgi: Common Gateway Interface plugin for the Caddy HTTP server ), but I did not understand at last how this plugin fork a process. I couldn’t find out any go code like “exec.Command()” in this repository. Then, how it forks a CGI process?

caddy-cgi lets the Go standard library handle this part. See host.go.

Thnak you jung.

so you can pass the current working directory with the directive

env PWD={.}

Yes, at now I’m using this way.
But what I really want to do is to set CGI script’s working directory by Caddyfile, like “WorkingDirectory” directive of systemd service file.
I think it’s impossible in current version of caddy-cgi.
(Sorry, “I want to know how caddy-cgi passes PWD to CGI scripts.” was a confused question.)

caddy-cgi lets the Go standard library handle this part. See host.go.

Thanks.
I will read caddy-cgi again with that in mind.

But what I really want to do is to set CGI script’s working directory by Caddyfile, like “WorkingDirectory” directive of systemd service file.

Ah, I see.

I think it’s impossible in current version of caddy-cgi.

Unfortunately, yes, it is currently not possible. I will add an optional dir directive to support this and will report back here when that is complete.

1 Like

Thnaks for your advise, I understand how document root is passed to CGI as working directory on execute.
( I think cgi.go#L106 is the answer caddy-cgi/cgi.go at master · jung-kurt/caddy-cgi · GitHub )

I will add an optional dir directive to support this and will report back here when that is complete.

Thnak you so much!

The dir directive is now supported in caddy-cgi v1.11.0. It has been successfully deployed to the caddy build server.

1 Like

Thanks!
I’ll try that.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.