-
Notifications
You must be signed in to change notification settings - Fork 855
Expand file tree
/
Copy pathInkAPI.cc
More file actions
9213 lines (7617 loc) · 270 KB
/
InkAPI.cc
File metadata and controls
9213 lines (7617 loc) · 270 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
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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @file
Implements the Traffic Server C API functions.
@section license License
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <atomic>
#include <tuple>
#include <unordered_map>
#include <string_view>
#include <string>
#include <charconv>
#include "iocore/net/NetVConnection.h"
#include "iocore/net/NetHandler.h"
#include "iocore/net/UDPNet.h"
#include "tscore/ink_config.h"
#include "tscore/ink_platform.h"
#include "tscore/ink_base64.h"
#include "tscore/Encoding.h"
#include "tscore/PluginUserArgs.h"
#include "tscore/Layout.h"
#include "tscore/Diags.h"
#include "tsutil/Metrics.h"
#include "api/InkAPIInternal.h"
#include "api/HttpAPIHooks.h"
#include "proxy/logging/Log.h"
#include "proxy/hdrs/URL.h"
#include "proxy/hdrs/MIME.h"
#include "proxy/hdrs/HTTP.h"
#include "proxy/ProxySession.h"
#include "proxy/http2/Http2ClientSession.h"
#include "proxy/PoolableSession.h"
#include "proxy/http/HttpSM.h"
#include "proxy/http/HttpConfig.h"
#include "proxy/http/OverridableConfigDefs.h"
#include "proxy/PluginHttpConnect.h"
#include "../iocore/net/P_Net.h"
#include "../iocore/net/P_UnixNet.h"
#include "../iocore/net/P_SSLNetVConnection.h"
#include "../iocore/cache/P_CacheHttp.h"
#include "../iocore/cache/CacheVC.h"
#include "records/RecCore.h"
#include "../records/P_RecCore.h"
#include "../records/P_RecDefs.h"
#include "../iocore/net/P_SSLConfig.h"
#include "../iocore/net/P_SSLClientUtils.h"
#include "iocore/dns/DNSProcessor.h"
#include "iocore/net/ConnectionTracker.h"
#include "iocore/net/SSLAPIHooks.h"
#include "iocore/net/SSLDiags.h"
#include "iocore/net/TLSBasicSupport.h"
#include "iocore/net/TLSSNISupport.h"
#include "iocore/eventsystem/ConfigProcessor.h"
#include "proxy/Plugin.h"
#include "proxy/logging/LogObject.h"
#include "proxy/logging/LogConfig.h"
#include "proxy/PluginVC.h"
#include "proxy/http/HttpSessionAccept.h"
#include "proxy/PluginVC.h"
#include "proxy/FetchSM.h"
#include "api/LifecycleAPIHooks.h"
#include "proxy/http/HttpDebugNames.h"
#include "iocore/aio/AIO.h"
#include "iocore/eventsystem/Tasks.h"
#include "../iocore/net/P_OCSPStapling.h"
#include "records/RecDefs.h"
#include "records/RecCore.h"
#include "records/RecYAMLDecoder.h"
#include "iocore/utils/Machine.h"
#include "proxy/http/HttpProxyServerMain.h"
#include "shared/overridable_txn_vars.h"
#include "mgmt/config/FileManager.h"
#include "mgmt/rpc/jsonrpc/JsonRPC.h"
#include <swoc/bwf_base.h>
#include <swoc/IPRange.h>
#include "ts/ts.h"
/****************************************************************
* IMPORTANT - READ ME
* Any plugin using the IO Core must enter
* with a held mutex. SDK 1.0, 1.1 & 2.0 did not
* have this restriction so we need to add a mutex
* to Plugin's Continuation if it tries to use the IOCore
* Not only does the plugin have to have a mutex
* before entering the IO Core. The mutex needs to be held.
* We now take out the mutex on each call to ensure it is
* held for the entire duration of the IOCore call
***************************************************************/
// helper macro for setting HTTPHdr data
#define SET_HTTP_HDR(_HDR, _BUF_PTR, _OBJ_PTR) \
_HDR.m_heap = ((HdrHeapSDKHandle *)_BUF_PTR)->m_heap; \
_HDR.m_http = (HTTPHdrImpl *)_OBJ_PTR; \
_HDR.m_mime = _HDR.m_http->m_fields_impl;
// This assert is for internal API use only.
#if TS_USE_FAST_SDK
#define sdk_assert(EX) (void)(EX)
#else
#define sdk_assert(EX) ((void)((EX) ? (void)0 : _TSReleaseAssert(#EX, __FILE__, __LINE__)))
#endif
namespace rpc
{
extern std::mutex g_rpcHandlingMutex;
extern std::condition_variable g_rpcHandlingCompletion;
extern swoc::Rv<YAML::Node> g_rpcHandlerResponseData;
extern bool g_rpcHandlerProcessingCompleted;
} // namespace rpc
static ts::Metrics &global_api_metrics = ts::Metrics::instance();
ConfigUpdateCbTable *global_config_cbs = nullptr;
// Fetchpages SM
extern ClassAllocator<FetchSM, false> FetchSMAllocator;
/* From proxy/http/HttpProxyServerMain.c: */
extern bool ssl_register_protocol(const char *, Continuation *);
// External converters.
extern MgmtConverter const &HttpDownServerCacheTimeConv;
extern HttpSessionAccept *plugin_http_accept;
extern HttpSessionAccept *plugin_http_transparent_accept;
extern thread_local PluginThreadContext *pluginThreadContext;
extern ClassAllocator<INKContInternal, false> INKContAllocator;
extern ClassAllocator<INKVConnInternal, false> INKVConnAllocator;
static ClassAllocator<MIMEFieldSDKHandle, false> mHandleAllocator("MIMEFieldSDKHandle");
// forward declarations
TSReturnCode sdk_sanity_check_null_ptr(void const *ptr);
TSReturnCode sdk_sanity_check_mbuffer(TSMBuffer bufp);
namespace
{
DbgCtl dbg_ctl_plugin{"plugin"};
DbgCtl dbg_ctl_parent_select{"parent_select"};
DbgCtl dbg_ctl_iocore_net{"iocore_net"};
DbgCtl dbg_ctl_cache_url{"cache_url"};
DbgCtl dbg_ctl_ssl{"ssl"};
DbgCtl dbg_ctl_ssl_cert_update{"ssl.cert_update"};
DbgCtl dbg_ctl_ssl_session_cache_insert{"ssl.session_cache.insert"};
DbgCtl dbg_ctl_rpc_api{"rpc.api"};
} // end anonymous namespace
/******************************************************/
/* Allocators for field handles and standalone fields */
/******************************************************/
static MIMEFieldSDKHandle *
sdk_alloc_field_handle(TSMBuffer /* bufp ATS_UNUSED */, MIMEHdrImpl *mh)
{
MIMEFieldSDKHandle *handle = THREAD_ALLOC(mHandleAllocator, this_thread());
// TODO: Should remove this when memory allocation can't fail.
sdk_assert(sdk_sanity_check_null_ptr((void *)handle) == TS_SUCCESS);
obj_init_header(handle, HdrHeapObjType::FIELD_SDK_HANDLE, sizeof(MIMEFieldSDKHandle), 0);
handle->mh = mh;
return handle;
}
static void
sdk_free_field_handle(TSMBuffer bufp, MIMEFieldSDKHandle *field_handle)
{
if (sdk_sanity_check_mbuffer(bufp) == TS_SUCCESS) {
THREAD_FREE(field_handle, mHandleAllocator, this_thread());
}
}
// Defines in InkAPIInternal.cc
extern char traffic_server_version[128];
extern int ts_major_version;
extern int ts_minor_version;
extern int ts_patch_version;
/** Reservation for a user arg.
*/
struct UserArg {
TSUserArgType type;
std::string name; ///< Name of reserving plugin.
std::string description; ///< Description of use for this arg.
};
// Managing the user args tables, and the global storage (which is assumed to be the biggest, by far).
UserArg UserArgTable[TS_USER_ARGS_COUNT][MAX_USER_ARGS[TS_USER_ARGS_GLB]];
static PluginUserArgs<TS_USER_ARGS_GLB> global_user_args;
std::atomic<int> UserArgIdx[TS_USER_ARGS_COUNT]; // Table of next reserved index.
////////////////////////////////////////////////////////////////////
//
// API error logging
//
////////////////////////////////////////////////////////////////////
void
TSStatus(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
StatusV(fmt, args);
va_end(args);
}
void
TSNote(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
NoteV(fmt, args);
va_end(args);
}
void
TSWarning(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
WarningV(fmt, args);
va_end(args);
}
void
TSError(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
ErrorV(fmt, args);
va_end(args);
}
void
TSFatal(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
FatalV(fmt, args);
va_end(args);
}
void
TSAlert(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
AlertV(fmt, args);
va_end(args);
}
void
TSEmergency(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
EmergencyV(fmt, args);
va_end(args);
}
// Assert in debug AND optim
void
_TSReleaseAssert(const char *text, const char *file, int line)
{
_ink_assert(text, file, line);
}
// Assert only in debug
int
#ifdef DEBUG
_TSAssert(const char *text, const char *file, int line)
{
_ink_assert(text, file, line);
return 0;
}
#else
_TSAssert(const char *, const char *, int)
{
return 0;
}
#endif
////////////////////////////////////////////////////////////////////
//
// SDK Interoperability Support
//
// ----------------------------------------------------------------
//
// Standalone Fields (SDK Version-Interoperability Hack)
//
//
// A "standalone" field is an ugly hack for portability with old
// versions of the SDK that mirrored the old header system. In
// the old system, you could create arbitrary tiny little field
// objects, distinct from MIME header objects, and link them
// together. In the new header system, all fields are internal
// constituents of the MIME header. To preserve the semantics of
// the old SDK, we need to maintain the concept of fields that
// are created outside of a MIME header. Whenever a field is
// "attached" to a MIME header, it is copied into the MIME header
// field's slot, and the handle to the field is updated to refer
// to the new field.
//
// Hopefully, we can eliminate this old compatibility interface and
// migrate users to the newer semantics quickly.
//
// ----------------------------------------------------------------
//
// MIMEField SDK Handles (SDK Version-Interoperability Hack)
//
// MIMEField "handles" are used by the SDK as an indirect reference
// to the MIMEField. Because versions 1 & 2 of the SDK allowed
// standalone fields that existed without associated MIME headers,
// and because the version 3 SDK requires an associated MIME header
// for all field mutation operations (for presence bits, etc.) we
// need a data structure that:
//
// * identifies standalone fields and stores field name/value
// information for fields that are not yet in a header
// * redirects the field to a real header field when the field
// is inserted into a header
// * maintains the associated MIMEHdrImpl when returning field
// slots from lookup and create functions
//
// If the MIMEHdrImpl pointer is null, then the handle points
// to a standalone field, otherwise the handle points to a field
// within the MIME header.
//
////////////////////////////////////////////////////////////////////
/*****************************************************************/
/* Handles to headers are impls, but need to handle MIME or HTTP */
/*****************************************************************/
inline MIMEHdrImpl *
_hdr_obj_to_mime_hdr_impl(HdrHeapObjImpl *obj)
{
MIMEHdrImpl *impl;
if (static_cast<HdrHeapObjType>(obj->m_type) == HdrHeapObjType::HTTP_HEADER) {
impl = static_cast<HTTPHdrImpl *>(obj)->m_fields_impl;
} else if (static_cast<HdrHeapObjType>(obj->m_type) == HdrHeapObjType::MIME_HEADER) {
impl = static_cast<MIMEHdrImpl *>(obj);
} else {
ink_release_assert(!"mloc not a header type");
impl = nullptr; /* gcc does not know about 'ink_release_assert' - make it happy */
}
return impl;
}
inline MIMEHdrImpl *
_hdr_mloc_to_mime_hdr_impl(TSMLoc mloc)
{
return _hdr_obj_to_mime_hdr_impl(reinterpret_cast<HdrHeapObjImpl *>(mloc));
}
TSReturnCode
sdk_sanity_check_field_handle(TSMLoc field, TSMLoc parent_hdr = nullptr)
{
if (field == TS_NULL_MLOC) {
return TS_ERROR;
}
MIMEFieldSDKHandle *field_handle = reinterpret_cast<MIMEFieldSDKHandle *>(field);
if (static_cast<HdrHeapObjType>(field_handle->m_type) != HdrHeapObjType::FIELD_SDK_HANDLE) {
return TS_ERROR;
}
if (parent_hdr != nullptr) {
MIMEHdrImpl *mh = _hdr_mloc_to_mime_hdr_impl(parent_hdr);
if (field_handle->mh != mh) {
return TS_ERROR;
}
}
return TS_SUCCESS;
}
TSReturnCode
sdk_sanity_check_mbuffer(TSMBuffer bufp)
{
HdrHeapSDKHandle *handle = reinterpret_cast<HdrHeapSDKHandle *>(bufp);
if ((handle == nullptr) || (handle->m_heap == nullptr) || (handle->m_heap->m_magic != HdrBufMagic::ALIVE)) {
return TS_ERROR;
}
return TS_SUCCESS;
}
TSReturnCode
sdk_sanity_check_mime_hdr_handle(TSMLoc field)
{
if (field == TS_NULL_MLOC) {
return TS_ERROR;
}
MIMEFieldSDKHandle *field_handle = reinterpret_cast<MIMEFieldSDKHandle *>(field);
if (static_cast<HdrHeapObjType>(field_handle->m_type) != HdrHeapObjType::MIME_HEADER) {
return TS_ERROR;
}
return TS_SUCCESS;
}
TSReturnCode
sdk_sanity_check_url_handle(TSMLoc field)
{
if (field == TS_NULL_MLOC) {
return TS_ERROR;
}
MIMEFieldSDKHandle *field_handle = reinterpret_cast<MIMEFieldSDKHandle *>(field);
if (static_cast<HdrHeapObjType>(field_handle->m_type) != HdrHeapObjType::URL) {
return TS_ERROR;
}
return TS_SUCCESS;
}
TSReturnCode
sdk_sanity_check_http_hdr_handle(TSMLoc field)
{
if (field == TS_NULL_MLOC) {
return TS_ERROR;
}
HTTPHdrImpl *field_handle = reinterpret_cast<HTTPHdrImpl *>(field);
if (static_cast<HdrHeapObjType>(field_handle->m_type) != HdrHeapObjType::HTTP_HEADER) {
return TS_ERROR;
}
return TS_SUCCESS;
}
TSReturnCode
sdk_sanity_check_continuation(TSCont cont)
{
if ((cont == nullptr) || (reinterpret_cast<INKContInternal *>(cont)->m_free_magic == INKCONT_INTERN_MAGIC_DEAD)) {
return TS_ERROR;
}
return TS_SUCCESS;
}
TSReturnCode
sdk_sanity_check_fetch_sm(TSFetchSM fetch_sm)
{
if (fetch_sm == nullptr) {
return TS_ERROR;
}
return TS_SUCCESS;
}
TSReturnCode
sdk_sanity_check_http_ssn(TSHttpSsn ssnp)
{
if (ssnp == nullptr) {
return TS_ERROR;
}
return TS_SUCCESS;
}
TSReturnCode
sdk_sanity_check_txn(TSHttpTxn txnp)
{
if ((txnp != nullptr) && ((reinterpret_cast<HttpSM *>(txnp))->magic == HttpSmMagic_t::ALIVE)) {
return TS_SUCCESS;
}
return TS_ERROR;
}
TSReturnCode
sdk_sanity_check_mime_parser(TSMimeParser parser)
{
if (parser == nullptr) {
return TS_ERROR;
}
return TS_SUCCESS;
}
TSReturnCode
sdk_sanity_check_http_parser(TSHttpParser parser)
{
if (parser == nullptr) {
return TS_ERROR;
}
return TS_SUCCESS;
}
TSReturnCode
sdk_sanity_check_alt_info(TSHttpAltInfo info)
{
if (info == nullptr) {
return TS_ERROR;
}
return TS_SUCCESS;
}
TSReturnCode
sdk_sanity_check_hook_id(TSHttpHookID id)
{
return HttpAPIHooks::is_valid(id) ? TS_SUCCESS : TS_ERROR;
}
TSReturnCode
sdk_sanity_check_lifecycle_hook_id(TSLifecycleHookID id)
{
return LifecycleAPIHooks::is_valid(id) ? TS_SUCCESS : TS_ERROR;
}
TSReturnCode
sdk_sanity_check_ssl_hook_id(TSHttpHookID id)
{
if (id < TS_SSL_FIRST_HOOK || id > TS_SSL_LAST_HOOK) {
return TS_ERROR;
}
return TS_SUCCESS;
}
TSReturnCode
sdk_sanity_check_null_ptr(void const *ptr)
{
return ptr == nullptr ? TS_ERROR : TS_SUCCESS;
}
// Plugin metric IDs index the plugin RSB, so bounds check against that.
static TSReturnCode
sdk_sanity_check_stat_id(int id)
{
return (global_api_metrics.valid(id) ? TS_SUCCESS : TS_ERROR);
}
static TSReturnCode
sdk_sanity_check_rpc_handler_options(const TSRPCHandlerOptions *opt)
{
if (nullptr == opt) {
return TS_ERROR;
}
if (opt->auth.restricted < 0 || opt->auth.restricted > 1) {
return TS_ERROR;
}
return TS_SUCCESS;
}
/**
The function checks if the buffer is Modifiable and returns true if
it is modifiable, else returns false.
*/
bool
isWriteable(TSMBuffer bufp)
{
if (bufp != nullptr) {
return (reinterpret_cast<HdrHeapSDKHandle *>(bufp))->m_heap->m_writeable;
}
return false;
}
////////////////////////////////////////////////////////////////////
//
// API memory management
//
////////////////////////////////////////////////////////////////////
void *
_TSmalloc(size_t size, const char * /* path ATS_UNUSED */)
{
return ats_malloc(size);
}
void *
_TSrealloc(void *ptr, size_t size, const char * /* path ATS_UNUSED */)
{
return ats_realloc(ptr, size);
}
// length has to be int64_t and not size_t, since -1 means to call strlen() to get length
char *
_TSstrdup(const char *str, int64_t length, const char *path)
{
return _xstrdup(str, length, path);
}
size_t
TSstrlcpy(char *dst, const char *str, size_t siz)
{
return ink_strlcpy(dst, str, siz);
}
size_t
TSstrlcat(char *dst, const char *str, size_t siz)
{
return ink_strlcat(dst, str, siz);
}
void
TSfree(void *ptr)
{
ats_free(ptr);
}
////////////////////////////////////////////////////////////////////
//
// Encoding utility
//
////////////////////////////////////////////////////////////////////
TSReturnCode
TSBase64Decode(const char *str, size_t str_len, unsigned char *dst, size_t dst_size, size_t *length)
{
sdk_assert(sdk_sanity_check_null_ptr((void *)str) == TS_SUCCESS);
sdk_assert(sdk_sanity_check_null_ptr((void *)dst) == TS_SUCCESS);
return ats_base64_decode(str, str_len, dst, dst_size, length) ? TS_SUCCESS : TS_ERROR;
}
TSReturnCode
TSBase64Encode(const char *str, size_t str_len, char *dst, size_t dst_size, size_t *length)
{
sdk_assert(sdk_sanity_check_null_ptr((void *)str) == TS_SUCCESS);
sdk_assert(sdk_sanity_check_null_ptr((void *)dst) == TS_SUCCESS);
return ats_base64_encode(str, str_len, dst, dst_size, length) ? TS_SUCCESS : TS_ERROR;
}
////////////////////////////////////////////////////////////////////
//
// API utility routines
//
////////////////////////////////////////////////////////////////////
ink_hrtime
TShrtime()
{
return ink_get_hrtime();
}
////////////////////////////////////////////////////////////////////
//
// API install and plugin locations
//
////////////////////////////////////////////////////////////////////
const char *
TSInstallDirGet()
{
static std::string prefix = Layout::get()->prefix;
return prefix.c_str();
}
const char *
TSConfigDirGet()
{
static std::string sysconfdir = RecConfigReadConfigDir();
return sysconfdir.c_str();
}
const char *
TSRuntimeDirGet()
{
static std::string runtimedir = RecConfigReadRuntimeDir();
return runtimedir.c_str();
}
const char *
TSTrafficServerVersionGet()
{
return traffic_server_version;
}
int
TSTrafficServerVersionGetMajor()
{
return ts_major_version;
}
int
TSTrafficServerVersionGetMinor()
{
return ts_minor_version;
}
int
TSTrafficServerVersionGetPatch()
{
return ts_patch_version;
}
const char *
TSPluginDirGet()
{
static std::string path = RecConfigReadPluginDir();
return path.c_str();
}
////////////////////////////////////////////////////////////////////
//
// Plugin registration
//
////////////////////////////////////////////////////////////////////
TSReturnCode
TSPluginRegister(const TSPluginRegistrationInfo *plugin_info)
{
sdk_assert(sdk_sanity_check_null_ptr((void *)plugin_info) == TS_SUCCESS);
if (!plugin_reg_current) {
return TS_ERROR;
}
plugin_reg_current->plugin_registered = true;
if (plugin_info->plugin_name) {
plugin_reg_current->plugin_name = ats_strdup(plugin_info->plugin_name);
}
if (plugin_info->vendor_name) {
plugin_reg_current->vendor_name = ats_strdup(plugin_info->vendor_name);
}
if (plugin_info->support_email) {
plugin_reg_current->support_email = ats_strdup(plugin_info->support_email);
}
return TS_SUCCESS;
}
TSReturnCode
TSPluginDSOReloadEnable(int enabled)
{
TSReturnCode ret = TS_SUCCESS;
if (!plugin_reg_current) {
return TS_ERROR;
}
if (!enabled) {
if (!PluginDso::loadedPlugins()->addPluginPathToDsoOptOutTable(plugin_reg_current->plugin_path)) {
ret = TS_ERROR;
}
}
return ret;
}
////////////////////////////////////////////////////////////////////
//
// API file management
//
////////////////////////////////////////////////////////////////////
TSFile
TSfopen(const char *filename, const char *mode)
{
FileImpl *file;
file = new FileImpl;
if (!file->fopen(filename, mode)) {
delete file;
return nullptr;
}
return reinterpret_cast<TSFile>(file);
}
void
TSfclose(TSFile filep)
{
FileImpl *file = reinterpret_cast<FileImpl *>(filep);
file->fclose();
delete file;
}
ssize_t
TSfread(TSFile filep, void *buf, size_t length)
{
FileImpl *file = reinterpret_cast<FileImpl *>(filep);
return file->fread(buf, length);
}
ssize_t
TSfwrite(TSFile filep, const void *buf, size_t length)
{
FileImpl *file = reinterpret_cast<FileImpl *>(filep);
return file->fwrite(buf, length);
}
void
TSfflush(TSFile filep)
{
FileImpl *file = reinterpret_cast<FileImpl *>(filep);
file->fflush();
}
char *
TSfgets(TSFile filep, char *buf, size_t length)
{
FileImpl *file = reinterpret_cast<FileImpl *>(filep);
return file->fgets(buf, length);
}
////////////////////////////////////////////////////////////////////
//
// Header component object handles
//
////////////////////////////////////////////////////////////////////
TSReturnCode
TSHandleMLocRelease(TSMBuffer bufp, TSMLoc parent, TSMLoc mloc)
{
MIMEFieldSDKHandle *field_handle;
HdrHeapObjImpl *obj = reinterpret_cast<HdrHeapObjImpl *>(mloc);
if (mloc == TS_NULL_MLOC) {
return TS_SUCCESS;
}
sdk_assert(sdk_sanity_check_mbuffer(bufp) == TS_SUCCESS);
switch (static_cast<HdrHeapObjType>(obj->m_type)) {
case HdrHeapObjType::URL:
case HdrHeapObjType::HTTP_HEADER:
case HdrHeapObjType::MIME_HEADER:
return TS_SUCCESS;
case HdrHeapObjType::FIELD_SDK_HANDLE:
field_handle = static_cast<MIMEFieldSDKHandle *>(obj);
if (sdk_sanity_check_field_handle(mloc, parent) != TS_SUCCESS) {
return TS_ERROR;
}
sdk_free_field_handle(bufp, field_handle);
return TS_SUCCESS;
default:
ink_release_assert(!"invalid mloc");
return TS_ERROR;
}
}
////////////////////////////////////////////////////////////////////
//
// HdrHeaps (previously known as "Marshal Buffers")
//
////////////////////////////////////////////////////////////////////
// TSMBuffer: pointers to HdrHeapSDKHandle objects
TSMBuffer
TSMBufferCreate()
{
TSMBuffer bufp;
HdrHeapSDKHandle *new_heap = new HdrHeapSDKHandle;
new_heap->m_heap = new_HdrHeap();
bufp = reinterpret_cast<TSMBuffer>(new_heap);
// TODO: Should remove this when memory allocation is guaranteed to fail.
sdk_assert(sdk_sanity_check_mbuffer(bufp) == TS_SUCCESS);
return bufp;
}
TSReturnCode
TSMBufferDestroy(TSMBuffer bufp)
{
// Allow to modify the buffer only
// if bufp is modifiable. If bufp is not modifiable return
// TS_ERROR. If allowed, return TS_SUCCESS. Changed the
// return value of function from void to TSReturnCode.
if (!isWriteable(bufp)) {
return TS_ERROR;
}
sdk_assert(sdk_sanity_check_mbuffer(bufp) == TS_SUCCESS);
HdrHeapSDKHandle *sdk_heap = reinterpret_cast<HdrHeapSDKHandle *>(bufp);
sdk_heap->m_heap->destroy();
delete sdk_heap;
return TS_SUCCESS;
}
////////////////////////////////////////////////////////////////////
//
// URLs
//
////////////////////////////////////////////////////////////////////
// TSMBuffer: pointers to HdrHeapSDKHandle objects
// TSMLoc: pointers to URLImpl objects
TSReturnCode
TSUrlCreate(TSMBuffer bufp, TSMLoc *locp)
{
sdk_assert(sdk_sanity_check_mbuffer(bufp) == TS_SUCCESS);
sdk_assert(sdk_sanity_check_null_ptr(locp) == TS_SUCCESS);
if (isWriteable(bufp)) {
HdrHeap *heap = reinterpret_cast<HdrHeapSDKHandle *>(bufp)->m_heap;
*locp = reinterpret_cast<TSMLoc>(url_create(heap));
return TS_SUCCESS;
}
return TS_ERROR;
}
TSReturnCode
TSUrlClone(TSMBuffer dest_bufp, TSMBuffer src_bufp, TSMLoc src_url, TSMLoc *locp)
{
sdk_assert(sdk_sanity_check_mbuffer(src_bufp) == TS_SUCCESS);
sdk_assert(sdk_sanity_check_mbuffer(dest_bufp) == TS_SUCCESS);
sdk_assert(sdk_sanity_check_url_handle(src_url) == TS_SUCCESS);
sdk_assert(sdk_sanity_check_null_ptr(locp) == TS_SUCCESS);
if (!isWriteable(dest_bufp)) {
return TS_ERROR;
}
HdrHeap *s_heap, *d_heap;
URLImpl *s_url, *d_url;
s_heap = reinterpret_cast<HdrHeapSDKHandle *>(src_bufp)->m_heap;
d_heap = reinterpret_cast<HdrHeapSDKHandle *>(dest_bufp)->m_heap;
s_url = reinterpret_cast<URLImpl *>(src_url);
d_url = url_copy(s_url, s_heap, d_heap, (s_heap != d_heap));
*locp = reinterpret_cast<TSMLoc>(d_url);
return TS_SUCCESS;
}
TSReturnCode
TSUrlCopy(TSMBuffer dest_bufp, TSMLoc dest_obj, TSMBuffer src_bufp, TSMLoc src_obj)
{
sdk_assert(sdk_sanity_check_mbuffer(src_bufp) == TS_SUCCESS);
sdk_assert(sdk_sanity_check_mbuffer(dest_bufp) == TS_SUCCESS);
sdk_assert(sdk_sanity_check_url_handle(src_obj) == TS_SUCCESS);
sdk_assert(sdk_sanity_check_url_handle(dest_obj) == TS_SUCCESS);
if (!isWriteable(dest_bufp)) {
return TS_ERROR;
}
HdrHeap *s_heap, *d_heap;
URLImpl *s_url, *d_url;
s_heap = reinterpret_cast<HdrHeapSDKHandle *>(src_bufp)->m_heap;
d_heap = reinterpret_cast<HdrHeapSDKHandle *>(dest_bufp)->m_heap;
s_url = reinterpret_cast<URLImpl *>(src_obj);
d_url = reinterpret_cast<URLImpl *>(dest_obj);
url_copy_onto(s_url, s_heap, d_url, d_heap, (s_heap != d_heap));
return TS_SUCCESS;
}
void
TSUrlPrint(TSMBuffer bufp, TSMLoc obj, TSIOBuffer iobufp)
{
sdk_assert(sdk_sanity_check_mbuffer(bufp) == TS_SUCCESS);
sdk_assert(sdk_sanity_check_url_handle(obj) == TS_SUCCESS);
sdk_assert(sdk_sanity_check_iocore_structure(iobufp) == TS_SUCCESS);
MIOBuffer *b = reinterpret_cast<MIOBuffer *>(iobufp);
IOBufferBlock *blk;
int bufindex;
int tmp, dumpoffset;
int done;
URL u;
u.m_heap = (reinterpret_cast<HdrHeapSDKHandle *>(bufp))->m_heap;
u.m_url_impl = reinterpret_cast<URLImpl *>(obj);
dumpoffset = 0;
do {
blk = b->get_current_block();
if (!blk || blk->write_avail() == 0) {
b->add_block();
blk = b->get_current_block();
}