Skip to content

Commit

Permalink
Merge pull request #154 from ilovenoah/150-add-set-http_cookie
Browse files Browse the repository at this point in the history
150 add set http cookie
  • Loading branch information
ilovenoah authored Mar 9, 2024
2 parents 4d5a5b5 + e724fb5 commit d085ee4
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
2 changes: 2 additions & 0 deletions inc/CGIHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class CGIHandler {
const std::string &actPath);
bool setServerSoftware(const Request &request,
const std::string &actPath);
bool setHttpCookie(const Request &request,
const std::string &actPath);
CGIHandler::cgiphase getCGIPhase() const;
void setCGIPhase(CGIHandler::cgiphase phase);
CGIHandler::cgiphase detectCGIPhase() const;
Expand Down
20 changes: 20 additions & 0 deletions src/response/CGIHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ CGIHandler::_initMetaVarSetterVec() {
metaVarSetterVec.push_back(&CGIHandler::setServerPort);
metaVarSetterVec.push_back(&CGIHandler::setServerProtocol);
metaVarSetterVec.push_back(&CGIHandler::setServerSoftware);
metaVarSetterVec.push_back(&CGIHandler::setHttpCookie);
return metaVarSetterVec;
}

Expand Down Expand Up @@ -336,6 +337,25 @@ bool CGIHandler::setServerSoftware(const Request &request,
return true;
}

bool CGIHandler::setHttpCookie(const Request &request, const std::string &actPath) {
(void)actPath;
std::string httpCookie;
Result<std::string, bool> res = request.getHeaderValue("Cookie");
if (res.isError() == true) {
return true;
}
if (res.isOK() == true) {
httpCookie = res.getOk();
}
httpCookie = "HTTP_COOKIE=" + httpCookie;
char *httpCookiePtr = strDupToCharPtr(httpCookie);
if (httpCookiePtr == NULL) {
return false;
}
this->_env.push_back(httpCookiePtr);
return true;
}

bool CGIHandler::init(Request &request, Server &server,
std::string const &actPath) {
extern char **environ;
Expand Down
101 changes: 101 additions & 0 deletions tests/simple_server_function_test/www/cgi-bin/cookie.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/usr/local/bin/perl

# 1998.03.01 リニューアル初版
# 1998.11.08 IE4.0で正常に設定できない問題に対処
# 2004.06.11 曜日を修正。タグを小文字に修正。

#
# Cookieの値を得る
#
&getCookie();
$time = $COOKIE{'KAISUU'};
if ($time eq "") {
$time = 1;
}
$date = $COOKIE{'HIZUKE'};
if ($date eq "") {
$date = "???";
}

#
# 書き込むCookieの値を得る
#
$time_new = $time + 1;
($sec, $min, $hour, $mday, $mon, $year) = localtime();
$date_new = sprintf("%04d/%02d/%02d %02d:%02d:%02d",
$year + 1900, $mon + 1, $mday, $hour, $min, $sec);

#
# ページを表示する。
#
$setcook1 = &setCookie("KAISUU", $time_new);
$setcook2 = &setCookie("HIZUKE", $date_new);
print <<END_OF_DATA;
Content-type: text/html
$setcook1
$setcook2
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Cookie Test</title>
<style>
* { font-size: 10pt; font-family: Consolas; }
</style>
</head>
<body>
<h1>Cookieのテスト</h1>
<hr>
<h2>読み込んだCookieの値(整形前)</h2>
<pre>
HTTP_COOKIE: $ENV{'HTTP_COOKIE'}
</pre>
<hr>
<h2>読み込んだCookieの値(整形後)</h2>
<div>これまでの訪問回数:$time</div>
<div>前回の訪問日:$date</div>
<hr>
<h2>書き込んだCookieの値</h2>
<div>$setcook1</div>
<div>$setcook2</div>
<hr>
<p>再表\示してみてください。一度ブラウザを終了させても、値が保存されています。</p>
</body>
</html>
END_OF_DATA

#
# Cookieの値を読み出す
#
sub getCookie {
local($xx, $name, $value);
foreach $xx (split(/; */, $ENV{'HTTP_COOKIE'})) {
($name, $value) = split(/=/, $xx);
$value =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/pack("C", hex($1))/eg;
$COOKIE{$name} = $value;
}
}

#
# Cookieに値を書き込むためのSet-Cookie:ヘッダを生成する
#
sub setCookie {
local($tmp, $val);
$val = $_[1];
$val =~ s/(\W)/sprintf("%%%02X", unpack("C", $1))/eg;
$tmp = "Set-Cookie: ";
$tmp .= "$_[0]=$val; ";
$tmp .= "expires=Tue, 1-Jan-2030 00:00:00 GMT;";
return($tmp);
}

#
# Cookieを削除するためのSet-Cookie:ヘッダを生成する
#
sub clearCookie {
$tmp = "Set-Cookie: ";
$tmp .= "$_[0]=xx; ";
$tmp .= " expires=Tue, 1-Jan-1980 00:00:00 GMT;";
return($tmp);
}

0 comments on commit d085ee4

Please sign in to comment.