-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharticle-cookies.html
More file actions
executable file
·379 lines (342 loc) · 17.8 KB
/
article-cookies.html
File metadata and controls
executable file
·379 lines (342 loc) · 17.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<!DOCTYPE html>
<html lang="en">
<head>
<!--__RIM_REDIRECT__-->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Quicksand:wght@300..700&display=swap" rel="stylesheet">
<title>Cookies in RimStone applications, plus HAProxy</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="HandheldFriendly" content="True"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="canonical" href="https://rimstone-lang.com/article-cookies.html" />
<style>
body, html {
font-family: Quicksand,monospace,Helvetica, Arial, sans-serif;
line-height: 150%;
font-size:16px;
}
body {
padding-left:1vw;
padding-right:1vw;
}
.ncode {
letter-spacing: 0px;
font-family: monospace;
font-size:83%;
display:inline-block;
max-width:99%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:8px;
margin-top:8px;
border: 2px solid #d6d6d6;
background-color:#f5f7f4;
white-space:nowrap;
}
.shcode {
letter-spacing: 0px;
font-family: monospace;
font-size:83%;
display:inline-block;
max-width:99%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:8px;
margin-top:8px;
border: 2px solid #d6d6d6;
background-color:#f5f7f4;
white-space:nowrap;
}
.sqlcode {
letter-spacing: 0px;
font-family: monospace;
font-size:83%;
display:inline-block;
max-width:99%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:8px;
margin-top:8px;
border: 2px solid #d6d6d6;
background-color:#f5f7f4;
white-space:nowrap;
}
.htmlcode {
letter-spacing: 0px;
font-family: monospace;
font-size:83%;
display:inline-block;
max-width:99%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:8px;
margin-top:8px;
border: 2px solid #d6d6d6;
background-color:#f5f7f4;
white-space:nowrap;
}
.code {
letter-spacing: 0px;
font-family: monospace;
font-size:83%;
display:inline-block;
max-width:99%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:8px;
margin-top:8px;
border: 2px solid #d6d6d6;
background-color:#f5f7f4;
white-space:nowrap;
}
/*Just like h1 but for pdf conversion it would be indented this way it's not*/
.vhub {
display: block;
font-size: 1.6em;
margin-top: 0.63em;
margin-bottom: 0.63em;
margin-left: 0;
margin-right: 0;
font-weight: bold;
}
/*Just like h2 but for pdf conversion it would be indented this way it's not*/
.vsub {
display: block;
font-size: 1.25em;
margin-top: 0.53em;
margin-bottom: 0.53em;
margin-left: 0;
margin-right: 0;
font-weight: bold;
}
ul {
margin-left: 0.75vw;
padding-left: 0;
}
li {
margin-left: 0.75vw;
padding-left: 0;
}
/* this must be last, as it overrides previous settings, for mobile */
@media (hover: none) {
a {
display: inline-block;
padding-top: 3px;
padding-bottom: 2px;
}
body {
padding-left:2vw;
padding-right:2vw;
letter-spacing: 1px;
}
}
/*The following is for code snippets that are highlighted by 2html vim*/
pre { overflow-x: scroll; margin:0; padding:0; font-family:monospace; }
.Identifier { color: #008b8b; }
.Statement { color: #af5f00; }
.PreProc { color: #5fd7ff; }
.Type { color: #005f00; }
.Comment { color: blue ; }
.Constant { color: #ff00ff; }
/*end of highlighted snippets*/
a {
text-decoration:none;
padding-bottom: 0px;
color:inherit;
border-bottom: 2px solid #6cb8f0;
}
a:hover {
text-decoration: none;
color:black;
border-bottom: 1px solid red;
}
/*do not underline links nor should they be active*/
pre a {
text-decoration:none;
color:black;
border-bottom: none;
pointer-events: none;
cursor: default;
}
.golfSnippet {display:none;}
ul {
list-style-type:square;
list-style-position: outside;
}
</style>
</head>
<body>
<div id="google_translate_element" style='float:right'></div>
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
<script>
function rim_copy(gt, eid, gc) {
gt.textContent = eid.textContent;
gt.select();
document.execCommand("copy");
gc.style.visibility="visible"
setTimeout(()=>{ gc.style.visibility="hidden"; }, 1000);
}
function googleTranslateElementInit() {
new google.translate.TranslateElement({pageLanguage: 'en'}, 'google_translate_element');
}
</script>
<!--RIMMENU13-->
<!--RIMENDMENU13-->
<!--BEGVDOC90-->
<div class='vhub' style='margin-top:10px;margin-right:20px;text-align:left;background-color:white;'><a href='https://rimstone-lang.com' style='border-bottom:0px'><img src='https://rimstone-lang.com/rimstone.png' style='width:180px;height:auto'/></a></div><div class='vhub' style='margin-top:10px;'>Cookies in RimStone applications, plus HAProxy</div><hr/><br/>
Cookies are used in web development to remember the state of your application on a client device. This way, your end-user on this client device doesn't have to provide the same (presumably fairly constant) information all the time. A "client device" can be just a plain web browser, or it can be anything else, such as any internet connected device.<br/>
<br/>
In practicality, cookies are used to remember user name, session state, preferences and any other information that a web application wishes to keep on a client device. <br/>
<br/>
So cookies are quite important for any web application or service. In this example, we'll set a single cookie (name of the user), and then we will retrieve it later. Very simple, but it's the foundation of what you'd need to do in pretty much any application.<br/>
<br/>
First, in a separate directory, create an application "yum" (as in cookies are delicious):<br/>
<div class="shcode" style='position:relative;padding-right:16px;'>
<pre id='code_83' class=notranslate>
<span class="Statement">mkdir</span> cookies
<span class="Statement">cd</span> cookies
rim <span class="Special">-k</span> yum</pre>
<span id=rimstone_copied_83 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_83' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_83, code_83, rimstone_copied_83)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
Next, create this source code file "biscuit.rim" (for everyone outside the US who feels it should have been "biscuit" instead of "cookie"):<br/>
<div class='code' style='position:relative;padding-right:16px;'>
<pre id='code_82' class=notranslate>
cat << '<span class="Constant">EOF</span>' > biscuit.rim
<span class="Statement">begin-handler</span> /biscuit
<span class="Statement"> get-param</span> action
<span class="Statement"> if-true</span> action<span class="Identifier"> equal</span> <span class="Constant">"enter-cookie"</span>
<span class="Comment">// Display a form to get cookie value</span>
<span class="Statement"> @</span><h2>Enter your name</h2>
<span class="Statement"> @</span><form action="<span class="Statement"><<print-path</span> <span class="Constant">"/biscuit"</span><span class="Statement">>></span>" method="POST">
<span class="Statement"> @</span> <input type="hidden" name="action" value="save-cookie">
<span class="Statement"> @</span> <label for="cookie-value">Your name:</label><br/>
<span class="Statement"> @</span> <input type="text" name="cookie-value" value=""><br/>
<span class="Statement"> @</span> <br/>
<span class="Statement"> @</span> <input type="submit" value="Submit">
<span class="Statement"> @</span></form>
<span class="Statement"> else-if</span> action<span class="Identifier"> equal</span> <span class="Constant">"save-cookie"</span>
<span class="Comment">// Submittal of form: save the cookie through response to the browser</span>
<span class="Statement"> get-param</span> cookie_value
<span class="Statement"> get-time</span><span class="Type"> to</span> cookie_expiration<span class="Identifier"> year</span> <span class="Constant">1</span><span class="Identifier"> timezone</span> <span class="Constant">"GMT"</span>
<span class="Statement"> set-cookie</span> <span class="Constant">"customer-name"</span> = cookie_value <span class="Identifier"> expires</span> cookie_expiration <span class="Identifier"> path</span> <span class="Constant">"/"</span>
<span class="Statement"> @</span>Cookie sent to browser!
<span class="Statement"> @</span><hr/>
<span class="Statement"> else-if</span> action<span class="Identifier"> equal</span> <span class="Constant">"query-cookie"</span>
<span class="Comment">// Web request that delivers cookie value back here (to server); display it.</span>
<span class="Statement"> get-cookie</span> name=<span class="Constant">"customer-name"</span>
<span class="Statement"> @</span>Customer name is <span class="Statement"><<print-out</span> name<span class="Statement"> web-encode>></span>
<span class="Statement"> @</span><hr/>
<span class="Statement"> else-if</span>
<span class="Statement"> @</span>Unrecognized action<hr/>
<span class="Statement"> end-if</span>
<span class="Statement">end-handler</span>
<span class="Constant">EOF</span></pre>
<span id=rimstone_copied_82 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_82' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_82, code_82, rimstone_copied_82)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
The code is pretty easy: if "action" URL parameter is "enter-cookie", you can enter your name. Then, when you submit this web form (passing back "action" parameter with value "save-cookie"), the cookie named "customer-name" is saved with the value you entered, and the cookie expiration is set 1 year into the future. Then you'll query the cookie value in another request ("query-cookie" as "action" parameter), and you'll get your name back because it was saved on the client.<br/>
<br/>
Okay, so compile the application (with all request handlers public):<br/>
<div class="shcode" style='position:relative;padding-right:16px;'>
<pre id='code_85' class=notranslate>
rim <span class="Special">-q</span> <span class="Special">--public</span></pre>
<span id=rimstone_copied_85 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_85' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_85, code_85, rimstone_copied_85)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
Now, start the RimStone server to serve up your yummy application:<br/>
<div class="shcode" style='position:relative;padding-right:16px;'>
<pre id='code_86' class=notranslate>
mrim <span class="Special">-p</span> <span class="Constant">3000</span> yum</pre>
<span id=rimstone_copied_86 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_86' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_86, code_86, rimstone_copied_86)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
"-p 3000" means it will run on socket port 3000, and the reason for that will become apparent momentarily.<br/>
<br/>
We'll use HAProxy as a web server in this instance to demostrate that you can use RimStone with pretty much any web server or load balancer. To set it up, here is the simple config file (typically in "/etc/haproxy/haproxy.cfg"). Note that "frontend", "fcgi-app" and "backend" sections are relevant to us here; the rest can be as it is by default in whatever configuration file you have (substitute "your-user" for your OS user name):<br/>
<div class="shcode" style='position:relative;padding-right:16px;'>
<pre id='code_87' class=notranslate>
frontend front_server
mode http
bind *:<span class="Constant">90</span>
use_backend backend_servers <span class="Statement">if </span><span class="Special">{</span> path_reg <span class="Special">-i</span> ^.*<span class="Special">\/</span>yum<span class="Special">\/</span>.*$ <span class="Special">}</span>
option forwardfor
fcgi-app rim-fcgi
log-stderr global
docroot /home/your-user/.rimstone/apps/yum/app
path-info ^.+<span class="PreProc">(</span>/yum<span class="PreProc">)(</span>/.+<span class="PreProc">)</span>$
backend backend_servers
mode http
filter fcgi-app rim-fcgi
use-fcgi-app rim-fcgi
server s1 <span class="Constant">127</span>.<span class="Constant">0</span>.<span class="Constant">0</span>.<span class="Constant">1</span>:<span class="Constant">3000</span> proto fcgi</pre>
<span id=rimstone_copied_87 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_87' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_87, code_87, rimstone_copied_87)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
So, the HAProxy server will respond on port 90 from the web. We specify that it will redirect any URL starting with "/yum/" to your RimStone application. Obviously, if you change your application name from "yum" to something else, change "yum" here too. Then we specify we'd like to use FastCGI protocol, which RimStone uses because it's very fast and capable. And finally, we tell HAProxy we'd like to communicate with our server on port 3000. So that's why we used "-p 3000" above when starting the server!<br/>
<br/>
Restart HAProxy for this to take effect:<br/>
<div class="shcode" style='position:relative;padding-right:16px;'>
<pre id='code_88' class=notranslate>
sudo systemctl restart haproxy</pre>
<span id=rimstone_copied_88 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_88' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_88, code_88, rimstone_copied_88)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
Now you can test your application server. Open a browser, and point it to your server where all the above code took place. If you're testing this on your own local machine, then just use "127.0.0.1" - and that's what we'll do here:<br/>
<div class='ncode' style='position:relative;padding-right:16px;'>
<pre id='code_81' class=notranslate>
http://127.0.0.1:90/yum/biscuit/action=enter-cookie</pre>
<span id=rimstone_copied_81 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_81' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_81, code_81, rimstone_copied_81)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
You'd get something like this:<br/>
<br/><div class='vimg'><img class='imgs' src='cookies_enter.png' alt='RimStone'></img>
</div><br/>
Enter the name, and click Submit, and you'll get the message that the cookie is saved:<br/>
<br/><div class='vimg'><img class='imgs' src='cookies_sent.png' alt='RimStone'></img>
</div><br/>
Now, enter this URL to query the cookie you set:<br/>
<div class='ncode' style='position:relative;padding-right:16px;'>
<pre id='code_84' class=notranslate>
http://127.0.0.1:90/yum/biscuit/action=query-cookie</pre>
<span id=rimstone_copied_84 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_84' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_84, code_84, rimstone_copied_84)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
And you'll see:<br/>
<br/><div class='vimg'><img class='imgs' src='cookies_show.png' alt='RimStone'></img>
</div><br/>
So now you've learned how to use cookies in RimStone, and how to use HAProxy too. Of course, you could have used Apache, Nginx or some other web server as well.<br/>
<!--ENDVDOC90-->
<br/><div style='width:100%;clear:both;'>
<hr/>
<!--RIMFOOT77--><span style='font-size:80%'><a href="https://rimstone-lang.com/copyright.html">Copyright</a> (c) 2019-2025 Gliim LLC. All contents on this web site is "AS IS" without warranties or guarantees of any kind.</span>
</div><br/></body></html>