Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
#include "dbms/owa.h"
 
#include <string>
 
extern "C"
{
#include "access/xact.h"
#include "utils/builtins.h"
#include "utils/elog.h"
#include "utils/numeric.h"
#include "utils/timestamp.h"
}
 
namespace Owa
{
static std::string
normalizeCacheLevel(const std::string &cache_level)
{
    std::string normalizedLevel;
 
    normalizedLevel.reserve(cache_level.size());
    for (unsigned char ch : cache_level)
    {
        normalizedLevel.push_back(pg_toupper(ch));
    }
 
    return normalizedLevel;
}
 
void
validateCacheEtag(const std::string &etag_value)
{
    validateHeaderValue(etag_value, "p_etag");
 
    if (etag_value.length() > 55)
    {
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("ORA-06502: PL/SQL: numeric or value error"),
                 errdetail("OWA_CACHE.SET_CACHE parameter p_etag is longer than 55 characters.")));
    }
}
 
std::string
validateAndNormalizeCacheLevel(const std::string &level_value)
{
    std::string normalizedLevel;
 
    validateHeaderValue(level_value, "p_level");
    normalizedLevel = normalizeCacheLevel(level_value);
 
    if (normalizedLevel != "USER" && normalizedLevel != "SYSTEM")
    {
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("ORA-06502: PL/SQL: numeric or value error"),
                 errdetail("OWA_CACHE.SET_CACHE parameter p_level must be USER or SYSTEM.")));
    }
 
    return normalizedLevel;
}
 
std::string
renderCacheHeaders(const std::string &etag_value, const std::string &level_value)
{
    std::string out;
    const char *cacheScope = (level_value == "USER") ? "private" : "public";
 
    out.reserve(etag_value.size() + level_value.size() + 96);
    out += "ETag: \"";
    out += etag_value;
    out += "\"\n";
    out += "Cache-Control: no-cache, ";
    out += cacheScope;
    out += "\n";
 
    return out;
}
 
void
validateCacheExpires(int32 expires_value)
{
    if (expires_value <= 0 || expires_value > 525600)
    {
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("ORA-06502: PL/SQL: numeric or value error"),
                 errdetail("OWA_CACHE.SET_EXPIRES parameter p_expires must be between 1 and 525600.")));
    }
}
 
void
validateSurrogateControlValue(const std::string &value)
{
    validateHeaderValue(value, "p_value");
 
    if (value.length() > 55)
    {
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("ORA-06502: PL/SQL: numeric or value error"),
                 errdetail("OWA_CACHE.SET_SURROGATE_CONTROL parameter p_value is longer than 55 characters.")));
    }
}
 
std::string
renderExpiresHeaders(int32 expires_value, const std::string &level_value)
{
    TimestampTz expiresTimestamp;
    text *expiresText = nullptr;
    char *expiresCString = nullptr;
    std::string out;
    const char *cacheScope = (level_value == "USER") ? "private" : "public";
 
    expiresTimestamp = DatumGetTimestampTz(DirectFunctionCall2(timestamp_pl_interval,
                                                               TimestampTzGetDatum(GetCurrentTimestamp()),
                                                               DirectFunctionCall7(make_interval,
                                                                                   Int32GetDatum(0),
                                                                                   Int32GetDatum(0),
                                                                                   Int32GetDatum(0),
                                                                                   Int32GetDatum(0),
                                                                                   Int32GetDatum(0),
                                                                                   Int32GetDatum(expires_value),
                                                                                   Float8GetDatum(0.0))));
    expiresText = DatumGetTextPP(DirectFunctionCall2(timestamptz_to_char,
                                                     TimestampTzGetDatum(expiresTimestamp),
                                                     CStringGetTextDatum("Dy, DD Mon YYYY HH24:MI:SS \"GMT\"")));
    expiresCString = text_to_cstring(expiresText);
 
    out.reserve(strlen(expiresCString) + level_value.size() + 96);
    out += "Cache-Control: max-age=";
    out += std::to_string(expires_value * 60);
    out += ", ";
    out += cacheScope;
    out += "\n";
    out += "Expires: ";
    out += expiresCString;
    out += "\n";
 
    pfree(expiresCString);
    return out;
}
} /* namespace Owa */
 
extern "C"
{
PG_FUNCTION_INFO_V1(owa_cache_set_cache);
PG_FUNCTION_INFO_V1(owa_cache_set_expires);
PG_FUNCTION_INFO_V1(owa_cache_set_not_modified);
PG_FUNCTION_INFO_V1(owa_cache_disable);
PG_FUNCTION_INFO_V1(owa_cache_set_surrogate_control);
PG_FUNCTION_INFO_V1(owa_cache_set_request_etag);
PG_FUNCTION_INFO_V1(owa_cache_set_request_level);
PG_FUNCTION_INFO_V1(owa_cache_get_etag);
PG_FUNCTION_INFO_V1(owa_cache_get_etag2);
PG_FUNCTION_INFO_V1(owa_cache_get_level);
 
Datum
owa_cache_disable(PG_FUNCTION_ARGS)
{
    Owa::EventEntry ev;
    Owa::PageContext *ctx = Owa::current_page_context_;
 
    ereport(LOG,
            (errmsg("OWA_CACHE.DISABLE request"),
             errdetail("nargs=%d, ctx=%s",
                       PG_NARGS(),
                       ctx == nullptr ? "null" : "present")));
 
    if (ctx == nullptr)
    {
        ereport(LOG,
                (errmsg("OWA_CACHE.DISABLE initializing request context"),
                 errdetail("reason=no active OWA page context")));
        Owa::ensurePageContextViaServiceInit();
        ctx = Owa::current_page_context_;
    }
 
    if (ctx->state != Owa::PageState::HeadersOpen)
    {
        ereport(LOG,
                (errmsg("OWA_CACHE.DISABLE called after body started"),
                 errdetail("page_state=%s", "BodyStarted")));
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("OWA_CACHE.DISABLE must be called in HTTP header context")));
    }
 
    ev.kind = Owa::EventKind::Cache;
    ev.subxid = GetCurrentSubTransactionId();
    ev.cacheDisabled = true;
    ctx->events.push_back(std::move(ev));
 
    ereport(LOG,
            (errmsg("OWA_CACHE.DISABLE stored disable cache headers"),
             errdetail("events_count=%zu", ctx->events.size())));
 
    PG_RETURN_VOID();
}
 
Datum
owa_cache_set_cache(PG_FUNCTION_ARGS)
{
    Owa::EventEntry ev;
    Owa::PageContext *ctx = Owa::current_page_context_;
    std::string etagValue;
    std::string levelValue;
    std::string normalizedLevel;
 
    ereport(LOG,
            (errmsg("OWA_CACHE.SET_CACHE request"),
             errdetail("nargs=%d, ctx=%s",
                       PG_NARGS(),
                       ctx == nullptr ? "null" : "present")));
 
    if (ctx == nullptr)
    {
        ereport(LOG,
                (errmsg("OWA_CACHE.SET_CACHE initializing request context"),
                 errdetail("reason=no active OWA page context")));
        Owa::ensurePageContextViaServiceInit();
        ctx = Owa::current_page_context_;
    }
 
    if (PG_NARGS() < 2 || PG_ARGISNULL(0) || PG_ARGISNULL(1))
    {
        ereport(LOG,
                (errmsg("OWA_CACHE.SET_CACHE invalid NULL parameter"),
                 errdetail("p_etag_is_null=%s, p_level_is_null=%s",
                           (PG_NARGS() < 1 || PG_ARGISNULL(0)) ? "true" : "false",
                           (PG_NARGS() < 2 || PG_ARGISNULL(1)) ? "true" : "false")));
        ereport(ERROR,
                (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                 errmsg("ORA-06502: PL/SQL: numeric or value error"),
                 errdetail("OWA_CACHE.SET_CACHE parameters p_etag and p_level must not be NULL.")));
    }
 
    if (ctx->state != Owa::PageState::HeadersOpen)
    {
        ereport(LOG,
                (errmsg("OWA_CACHE.SET_CACHE called after body started"),
                 errdetail("page_state=%s", "BodyStarted")));
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("OWA_CACHE.SET_CACHE must be called in HTTP header context")));
    }
 
    etagValue = Owa::textToString(PG_GETARG_TEXT_PP(0));
    levelValue = Owa::textToString(PG_GETARG_TEXT_PP(1));
 
    ereport(LOG,
            (errmsg("OWA_CACHE.SET_CACHE parameters"),
             errdetail("p_etag=\"%s\", p_etag_length=%zu, p_level=\"%s\"",
                       etagValue.c_str(),
                       etagValue.length(),
                       levelValue.c_str())));
 
    Owa::validateCacheEtag(etagValue);
    normalizedLevel = Owa::validateAndNormalizeCacheLevel(levelValue);
 
    ereport(LOG,
            (errmsg("OWA_CACHE.SET_CACHE normalized"),
             errdetail("normalized_level=\"%s\"", normalizedLevel.c_str())));
 
    ev.kind = Owa::EventKind::Cache;
    ev.subxid = GetCurrentSubTransactionId();
    ev.cacheEtag = std::move(etagValue);
    ev.cacheLevel = std::move(normalizedLevel);
    ctx->events.push_back(std::move(ev));
 
    ereport(LOG,
            (errmsg("OWA_CACHE.SET_CACHE stored cache headers"),
             errdetail("events_count=%zu", ctx->events.size())));
 
    PG_RETURN_VOID();
}
 
Datum
owa_cache_set_expires(PG_FUNCTION_ARGS)
{
    Owa::EventEntry ev;
    Owa::PageContext *ctx = Owa::current_page_context_;
    Numeric expiresNumeric = nullptr;
    int32 expiresValue;
    std::string levelValue;
    std::string normalizedLevel;
 
    ereport(LOG,
            (errmsg("OWA_CACHE.SET_EXPIRES request"),
             errdetail("nargs=%d, ctx=%s",
                       PG_NARGS(),
                       ctx == nullptr ? "null" : "present")));
 
    if (ctx == nullptr)
    {
        ereport(LOG,
                (errmsg("OWA_CACHE.SET_EXPIRES initializing request context"),
                 errdetail("reason=no active OWA page context")));
        Owa::ensurePageContextViaServiceInit();
        ctx = Owa::current_page_context_;
    }
 
    if (PG_NARGS() < 2 || PG_ARGISNULL(0) || PG_ARGISNULL(1))
    {
        ereport(LOG,
                (errmsg("OWA_CACHE.SET_EXPIRES invalid NULL parameter"),
                 errdetail("p_expires_is_null=%s, p_level_is_null=%s",
                           (PG_NARGS() < 1 || PG_ARGISNULL(0)) ? "true" : "false",
                           (PG_NARGS() < 2 || PG_ARGISNULL(1)) ? "true" : "false")));
        ereport(ERROR,
                (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                 errmsg("ORA-06502: PL/SQL: numeric or value error"),
                 errdetail("OWA_CACHE.SET_EXPIRES parameters p_expires and p_level must not be NULL.")));
    }
 
    if (ctx->state != Owa::PageState::HeadersOpen)
    {
        ereport(LOG,
                (errmsg("OWA_CACHE.SET_EXPIRES called after body started"),
                 errdetail("page_state=%s", "BodyStarted")));
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("OWA_CACHE.SET_EXPIRES must be called in HTTP header context")));
    }
 
    expiresNumeric = PG_GETARG_NUMERIC(0);
    expiresValue = DatumGetInt32(DirectFunctionCall1(numeric_int4, NumericGetDatum(expiresNumeric)));
    levelValue = Owa::textToString(PG_GETARG_TEXT_PP(1));
 
    ereport(LOG,
            (errmsg("OWA_CACHE.SET_EXPIRES parameters"),
             errdetail("p_expires=%d, p_level=\"%s\"",
                       expiresValue,
                       levelValue.c_str())));
 
    Owa::validateCacheExpires(expiresValue);
    normalizedLevel = Owa::validateAndNormalizeCacheLevel(levelValue);
 
    ereport(LOG,
            (errmsg("OWA_CACHE.SET_EXPIRES normalized"),
             errdetail("normalized_level=\"%s\", p_expires=%d",
                       normalizedLevel.c_str(),
                       expiresValue)));
 
    ev.kind = Owa::EventKind::Cache;
    ev.subxid = GetCurrentSubTransactionId();
    ev.cacheLevel = std::move(normalizedLevel);
    ev.cacheExpires = expiresValue;
    ctx->events.push_back(std::move(ev));
 
    ereport(LOG,
            (errmsg("OWA_CACHE.SET_EXPIRES stored cache headers"),
             errdetail("events_count=%zu", ctx->events.size())));
 
    PG_RETURN_VOID();
}
 
Datum
owa_cache_set_request_etag(PG_FUNCTION_ARGS)
{
    Owa::PageContext *ctx = Owa::current_page_context_;
    std::string etagValue;
 
    ereport(LOG,
            (errmsg("OWA_CACHE.SET_REQUEST_ETAG request"),
             errdetail("nargs=%d, ctx=%s",
                       PG_NARGS(),
                       ctx == nullptr ? "null" : "present")));
 
    if (ctx == nullptr)
    {
        ereport(LOG,
                (errmsg("OWA_CACHE.SET_REQUEST_ETAG initializing request context"),
                 errdetail("reason=no active OWA page context")));
        Owa::ensurePageContextViaServiceInit();
        ctx = Owa::current_page_context_;
    }
 
    if (PG_NARGS() < 1 || PG_ARGISNULL(0))
    {
        ereport(LOG,
                (errmsg("OWA_CACHE.SET_REQUEST_ETAG invalid NULL parameter"),
                 errdetail("p_etag_is_null=%s",
                           (PG_NARGS() < 1 || PG_ARGISNULL(0)) ? "true" : "false")));
        ereport(ERROR,
                (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                 errmsg("ORA-06502: PL/SQL: numeric or value error"),
                 errdetail("OWA_CACHE.SET_REQUEST_ETAG parameter p_etag must not be NULL.")));
    }
 
    etagValue = Owa::textToString(PG_GETARG_TEXT_PP(0));
 
    ereport(LOG,
            (errmsg("OWA_CACHE.SET_REQUEST_ETAG parameters"),
             errdetail("p_etag=\"%s\", p_etag_length=%zu",
                       etagValue.c_str(),
                       etagValue.length())));
 
    Owa::validateHeaderValue(etagValue, "p_etag");
    if (etagValue.length() > 55)
    {
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("ORA-06502: PL/SQL: numeric or value error"),
                 errdetail("OWA_CACHE.SET_REQUEST_ETAG parameter p_etag is longer than 55 characters.")));
    }
 
    ctx->hasRequestCacheEtag = true;
    ctx->requestCacheEtag = std::move(etagValue);
 
    ereport(LOG,
            (errmsg("OWA_CACHE.SET_REQUEST_ETAG stored request cache context"),
             errdetail("etag_length=%zu", ctx->requestCacheEtag.length())));
 
    PG_RETURN_VOID();
}
 
Datum
owa_cache_set_surrogate_control(PG_FUNCTION_ARGS)
{
    Owa::EventEntry ev;
    Owa::PageContext *ctx = Owa::current_page_context_;
    std::string value;
 
    ereport(LOG,
            (errmsg("OWA_CACHE.SET_SURROGATE_CONTROL request"),
             errdetail("nargs=%d, ctx=%s",
                       PG_NARGS(),
                       ctx == nullptr ? "null" : "present")));
 
    if (ctx == nullptr)
    {
        ereport(LOG,
                (errmsg("OWA_CACHE.SET_SURROGATE_CONTROL initializing request context"),
                 errdetail("reason=no active OWA page context")));
        Owa::ensurePageContextViaServiceInit();
        ctx = Owa::current_page_context_;
    }
 
    if (PG_NARGS() < 1 || PG_ARGISNULL(0))
    {
        ereport(LOG,
                (errmsg("OWA_CACHE.SET_SURROGATE_CONTROL invalid NULL parameter"),
                 errdetail("p_value_is_null=%s",
                           (PG_NARGS() < 1 || PG_ARGISNULL(0)) ? "true" : "false")));
        ereport(ERROR,
                (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                 errmsg("ORA-06502: PL/SQL: numeric or value error"),
                 errdetail("OWA_CACHE.SET_SURROGATE_CONTROL parameter p_value must not be NULL.")));
    }
 
    if (ctx->state != Owa::PageState::HeadersOpen)
    {
        ereport(LOG,
                (errmsg("OWA_CACHE.SET_SURROGATE_CONTROL called after body started"),
                 errdetail("page_state=%s", "BodyStarted")));
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("OWA_CACHE.SET_SURROGATE_CONTROL must be called in HTTP header context")));
    }
 
    value = Owa::textToString(PG_GETARG_TEXT_PP(0));
 
    ereport(LOG,
            (errmsg("OWA_CACHE.SET_SURROGATE_CONTROL parameters"),
             errdetail("p_value=\"%s\", p_value_length=%zu",
                       value.c_str(),
                       value.length())));
 
    Owa::validateSurrogateControlValue(value);
 
    ev.kind = Owa::EventKind::Cache;
    ev.subxid = GetCurrentSubTransactionId();
    ev.cacheSurrogateControl = std::move(value);
    ctx->events.push_back(std::move(ev));
 
    ereport(LOG,
            (errmsg("OWA_CACHE.SET_SURROGATE_CONTROL stored surrogate control"),
             errdetail("events_count=%zu", ctx->events.size())));
 
    PG_RETURN_VOID();
}
 
Datum
owa_cache_set_not_modified(PG_FUNCTION_ARGS)
{
    Owa::PageContext *ctx = Owa::current_page_context_;
    const Owa::EventEntry *cacheEvent = nullptr;
    Owa::EventEntry ev;
    text *requestProtocolText = nullptr;
    std::string requestProtocol;
 
    ereport(LOG,
            (errmsg("OWA_CACHE.SET_NOT_MODIFIED request"),
             errdetail("nargs=%d, ctx=%s",
                       PG_NARGS(),
                       ctx == nullptr ? "null" : "present")));
 
    if (ctx == nullptr)
    {
        ereport(LOG,
                (errmsg("OWA_CACHE.SET_NOT_MODIFIED initializing request context"),
                 errdetail("reason=no active OWA page context")));
        Owa::ensurePageContextViaServiceInit();
        ctx = Owa::current_page_context_;
    }
 
    if (!ctx->hasRequestCacheEtag)
    {
        ereport(LOG,
                (errmsg("OWA_CACHE.SET_NOT_MODIFIED missing request etag"),
                 errdetail("request_cache_etag=absent")));
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("ORA-06502: PL/SQL: numeric or value error"),
                 errdetail("OWA_CACHE.SET_NOT_MODIFIED requires request If-None-Match etag.")));
    }
 
    if (ctx->state != Owa::PageState::HeadersOpen)
    {
        ereport(LOG,
                (errmsg("OWA_CACHE.SET_NOT_MODIFIED called after body started"),
                 errdetail("page_state=%s", "BodyStarted")));
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("OWA_CACHE.SET_NOT_MODIFIED must be called in HTTP header context")));
    }
 
    for (auto it = ctx->events.rbegin(); it != ctx->events.rend(); ++it)
    {
        if (it->kind != Owa::EventKind::Cache)
        {
            continue;
        }
 
        if (it->cacheEtag.empty())
        {
            continue;
        }
 
        cacheEvent = &(*it);
        break;
    }
 
    if (cacheEvent == nullptr)
    {
        ereport(LOG,
                (errmsg("OWA_CACHE.SET_NOT_MODIFIED missing response cache metadata"),
                 errdetail("request_etag=\"%s\"", ctx->requestCacheEtag.c_str())));
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("ORA-06502: PL/SQL: numeric or value error"),
                 errdetail("OWA_CACHE.SET_NOT_MODIFIED requires a prior OWA_CACHE.SET_CACHE call.")));
    }
 
    ereport(LOG,
            (errmsg("OWA_CACHE.SET_NOT_MODIFIED compare"),
             errdetail("request_etag=\"%s\", response_etag=\"%s\", response_level=\"%s\"",
                       ctx->requestCacheEtag.c_str(),
                       cacheEvent->cacheEtag.c_str(),
                       cacheEvent->cacheLevel.c_str())));
 
    if (ctx->requestCacheEtag != cacheEvent->cacheEtag)
    {
        ereport(LOG,
                (errmsg("OWA_CACHE.SET_NOT_MODIFIED etag mismatch"),
                 errdetail("request_etag=\"%s\", response_etag=\"%s\"",
                           ctx->requestCacheEtag.c_str(),
                           cacheEvent->cacheEtag.c_str())));
        PG_RETURN_VOID();
    }
 
    if (ctx->hasRequestCacheLevel && ctx->requestCacheLevel != cacheEvent->cacheLevel)
    {
        ereport(LOG,
                (errmsg("OWA_CACHE.SET_NOT_MODIFIED level mismatch"),
                 errdetail("request_level=\"%s\", response_level=\"%s\"",
                           ctx->requestCacheLevel.c_str(),
                           cacheEvent->cacheLevel.c_str())));
        PG_RETURN_VOID();
    }
 
    requestProtocolText = DatumGetTextPP(DirectFunctionCall1(owa_util_get_cgi_env,
                                                             CStringGetTextDatum("REQUEST_PROTOCOL")));
    if (requestProtocolText == nullptr)
    {
        ereport(LOG,
                (errmsg("OWA_CACHE.SET_NOT_MODIFIED missing request protocol"),
                 errdetail("request_etag=\"%s\", response_etag=\"%s\"",
                           ctx->requestCacheEtag.c_str(),
                           cacheEvent->cacheEtag.c_str())));
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("OWA_CACHE.SET_NOT_MODIFIED requires REQUEST_PROTOCOL for 304 response")));
    }
 
    requestProtocol = Owa::textToString(requestProtocolText);
    Owa::validateHeaderValue(requestProtocol, "request_protocol");
 
    ev.kind = Owa::EventKind::Status;
    ev.subxid = GetCurrentSubTransactionId();
    ev.statusCode = 304;
    ev.statusReason = "Not Modified";
    ctx->events.push_back(std::move(ev));
 
    ereport(LOG,
            (errmsg("OWA_CACHE.SET_NOT_MODIFIED stored status line"),
             errdetail("status_code=%d, reason=\"%s\", request_protocol=\"%s\", events_count=%zu",
                       304,
                       "Not Modified",
                       requestProtocol.c_str(),
                       ctx->events.size())));
 
    PG_RETURN_VOID();
}
 
Datum
owa_cache_set_request_level(PG_FUNCTION_ARGS)
{
    Owa::PageContext *ctx = Owa::current_page_context_;
    std::string levelValue;
    std::string normalizedLevel;
 
    ereport(LOG,
            (errmsg("OWA_CACHE.SET_REQUEST_LEVEL request"),
             errdetail("nargs=%d, ctx=%s",
                       PG_NARGS(),
                       ctx == nullptr ? "null" : "present")));
 
    if (ctx == nullptr)
    {
        ereport(LOG,
                (errmsg("OWA_CACHE.SET_REQUEST_LEVEL initializing request context"),
                 errdetail("reason=no active OWA page context")));
        Owa::ensurePageContextViaServiceInit();
        ctx = Owa::current_page_context_;
    }
 
    if (PG_NARGS() < 1 || PG_ARGISNULL(0))
    {
        ereport(LOG,
                (errmsg("OWA_CACHE.SET_REQUEST_LEVEL invalid NULL parameter"),
                 errdetail("p_level_is_null=%s",
                           (PG_NARGS() < 1 || PG_ARGISNULL(0)) ? "true" : "false")));
        ereport(ERROR,
                (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                 errmsg("ORA-06502: PL/SQL: numeric or value error"),
                 errdetail("OWA_CACHE.SET_REQUEST_LEVEL parameter p_level must not be NULL.")));
    }
 
    levelValue = Owa::textToString(PG_GETARG_TEXT_PP(0));
 
    ereport(LOG,
            (errmsg("OWA_CACHE.SET_REQUEST_LEVEL parameters"),
             errdetail("p_level=\"%s\"", levelValue.c_str())));
 
    Owa::validateHeaderValue(levelValue, "p_level");
    normalizedLevel.reserve(levelValue.size());
    for (unsigned char ch : levelValue)
    {
        normalizedLevel.push_back(pg_toupper(ch));
    }
 
    if (normalizedLevel != "USER" && normalizedLevel != "SYSTEM")
    {
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("ORA-06502: PL/SQL: numeric or value error"),
                 errdetail("OWA_CACHE.SET_REQUEST_LEVEL parameter p_level must be USER or SYSTEM.")));
    }
 
    ctx->hasRequestCacheLevel = true;
    ctx->requestCacheLevel = std::move(normalizedLevel);
 
    ereport(LOG,
            (errmsg("OWA_CACHE.SET_REQUEST_LEVEL stored request cache context"),
             errdetail("normalized_level=\"%s\"", ctx->requestCacheLevel.c_str())));
 
    PG_RETURN_VOID();
}
 
Datum
owa_cache_get_etag(PG_FUNCTION_ARGS)
{
    Owa::PageContext *ctx = Owa::current_page_context_;
 
    (void) fcinfo;
 
    ereport(LOG,
            (errmsg("OWA_CACHE.GET_ETAG request"),
             errdetail("ctx=%s", ctx == nullptr ? "null" : "present")));
 
    if (ctx == nullptr)
    {
        ereport(LOG,
                (errmsg("OWA_CACHE.GET_ETAG result"),
                 errdetail("etag=null, reason=no active OWA page context")));
        PG_RETURN_NULL();
    }
 
    if (!ctx->hasRequestCacheEtag)
    {
        ereport(LOG,
                (errmsg("OWA_CACHE.GET_ETAG result"),
                 errdetail("etag=null, reason=request cache etag is absent")));
        PG_RETURN_NULL();
    }
 
    ereport(LOG,
            (errmsg("OWA_CACHE.GET_ETAG result"),
             errdetail("etag=\"%s\", etag_length=%zu",
                       ctx->requestCacheEtag.c_str(),
                       ctx->requestCacheEtag.length())));
    PG_RETURN_TEXT_P(cstring_to_text(ctx->requestCacheEtag.c_str()));
}
 
Datum
owa_cache_get_etag2(PG_FUNCTION_ARGS)
{
    int32 serviceArg = 0;
 
    if (PG_NARGS() != 1)
    {
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("OWA_CACHE.GET_ETAG2 accepts exactly 1 argument")));
    }
 
    if (!PG_ARGISNULL(0))
    {
        serviceArg = PG_GETARG_INT32(0);
    }
    (void) serviceArg;
 
    return owa_cache_get_etag(fcinfo);
}
 
Datum
owa_cache_get_level(PG_FUNCTION_ARGS)
{
    Owa::PageContext *ctx = Owa::current_page_context_;
 
    (void) fcinfo;
 
    ereport(LOG,
            (errmsg("OWA_CACHE.GET_LEVEL request"),
             errdetail("ctx=%s", ctx == nullptr ? "null" : "present")));
 
    if (ctx == nullptr)
    {
        ereport(LOG,
                (errmsg("OWA_CACHE.GET_LEVEL result"),
                 errdetail("level=null, reason=no active OWA page context")));
        PG_RETURN_NULL();
    }
 
    if (!ctx->hasRequestCacheLevel)
    {
        ereport(LOG,
                (errmsg("OWA_CACHE.GET_LEVEL result"),
                 errdetail("level=null, reason=request cache level is absent")));
        PG_RETURN_NULL();
    }
 
    ereport(LOG,
            (errmsg("OWA_CACHE.GET_LEVEL result"),
             errdetail("level=\"%s\"", ctx->requestCacheLevel.c_str())));
    PG_RETURN_TEXT_P(cstring_to_text(ctx->requestCacheLevel.c_str()));
}
} /* extern "C" */