至少我认为这是用来表达我的问题的最佳方式。我是一名新的编程学生,老师在他的笔记中有这个例子,他从来没有能够达到。我正在做的是使用cgi脚本进行简单的表单提交。我使用WebStorm作为IDE,当我尝试在我的网页上提交表单时遇到了麻烦。以下是我正在使用的三个代码文件:
html页面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Testing Form submission</title>
</head>
<body>
<h2>Testing Simple Form</h2>
<form name="test_form" method="post" action="form_submission.sh">
First Name: <input type="text" name="firstName"/><br/>
Last Name: <input type="text" name="lastName"/><br/>
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>
cgi脚本:
#!/usr/bin/env bash
source cgi_vars.sh
# register all GET and POST variables
cgi_getvars BOTH ALL
echo "Content-type: text/html"
echo
echo "<html>"
echo "<head>"
echo "<title>Form Submitted</title>"
echo "</head>"
echo "<body>"
echo "<h2>The values you submitted were:</h2>"
echo "First Name: ${firstName}<br/>"
echo "Last Name: ${lastName}<br/>"
echo "</body>"
echo "</html>"
以及脚本引用的其他一些文件,但我不确切知道它的用途或用途......:
#!/usr/bin/env bash
# (internal) routine to store POST data
function cgi_get_POST_vars()
{
# check content type
# FIXME: not sure if we could handle uploads with this..
[ "${CONTENT_TYPE}" != "application/x-www-form-urlencoded" ] && \
echo "bash.cgi warning: you should probably use MIME type "\
"application/x-www-form-urlencoded!" 1>&2
# save POST variables (only first time this is called)
[ -z "$QUERY_STRING_POST" \
-a "$REQUEST_METHOD" = "POST" -a ! -z "$CONTENT_LENGTH" ] && \
read -n $CONTENT_LENGTH QUERY_STRING_POST
# prevent shell execution
local t
t=${QUERY_STRING_POST//%60//} # %60 = `
t=${t//\`//}
t=${t//\$(//}
t=${t//%24%28//} # %24 = $, %28 = (
QUERY_STRING_POST=${t}
return
}
# (internal) routine to decode urlencoded strings
function cgi_decodevar()
{
[ $# -ne 1 ] && return
local v t h
# replace all + with whitespace and append %%
t="${1//+/ }%%"
while [ ${#t} -gt 0 -a "${t}" != "%" ]; do
v="${v}${t%%\%*}" # digest up to the first %
t="${t#*%}" # remove digested part
# decode if there is anything to decode and if not at end of string
if [ ${#t} -gt 0 -a "${t}" != "%" ]; then
h=${t:0:2} # save first two chars
t="${t:2}" # remove these
v="${v}"`echo -e \\\\x${h}` # convert hex to special char
fi
done
# return decoded string
echo "${v}"
return
}
# routine to get variables from http requests
# usage: cgi_getvars method varname1 [.. varnameN]
# method is either GET or POST or BOTH
# the magic varible name ALL gets everything
function cgi_getvars()
{
[ $# -lt 2 ] && return
local q p k v s
# prevent shell execution
t=${QUERY_STRING//%60//} # %60 = `
t=${t//\`//}
t=${t//\$(//}
t=${t//%24%28//} # %24 = $, %28 = (
QUERY_STRING=${t}
# get query
case $1 in
GET)
[ ! -z "${QUERY_STRING}" ] && q="${QUERY_STRING}&"
;;
POST)
cgi_get_POST_vars
[ ! -z "${QUERY_STRING_POST}" ] && q="${QUERY_STRING_POST}&"
;;
BOTH)
[ ! -z "${QUERY_STRING}" ] && q="${QUERY_STRING}&"
cgi_get_POST_vars
[ ! -z "${QUERY_STRING_POST}" ] && q="${q}${QUERY_STRING_POST}&"
;;
esac
shift
s=" $* "
# parse the query data
while [ ! -z "$q" ]; do
p="${q%%&*}" # get first part of query string
k="${p%%=*}" # get the key (variable name) from it
v="${p#*=}" # get the value from it
q="${q#$p&*}" # strip first part from query string
# decode and evaluate var if requested
[ "$1" = "ALL" -o "${s/ $k /}" != "$s" ] && \
eval "$k=\"`cgi_decodevar \"$v\"`\""
done
return
}
因此,当我在WebStorm中并单击其中一个浏览器选项时,它将使用localhost:6334 / filepath拉开该文件。执行此操作后,当我尝试提交表单时,由于某种原因开始下载cgi脚本。但是,如果我导航到localhost,我注意到它已经准备好了所有文件,所以当我从那里单击打开html文件并提交表单时,它可以正常使用cgi。
我的代码中有什么东西搞砸了吗?或者获取html文件的两种方式之间有一些根本区别吗?或者我需要更改某些服务器文件中的某些设置?
答案 0 :(得分:0)
好吧,WebStorm内置的webserver(提供来自localhost:63342/filepath
的文件)是一个简单的静态Web服务器,像你这样的POST请求是使用静态文件处理程序处理的 - 即服务器只返回一个文件内容指定的URL - 仅此而已。您必须有一个不同的Web服务器在'just localhost'上提供文件,可以不同地处理POST请求...