xref: /trafficserver/plugins/authproxy/utils.h (revision a139fd19)
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #pragma once
20 
21 #include <ts/ts.h>
22 #include <netinet/in.h>
23 #include <memory>
24 
25 #define AuthLogDebug(fmt, ...) TSDebug("authproxy", "%s: " fmt, __func__, ##__VA_ARGS__)
26 #define AuthLogError(fmt, ...) TSError(fmt, ##__VA_ARGS__)
27 
28 template <typename T>
29 T *
AuthNew()30 AuthNew()
31 {
32   return new (TSmalloc(sizeof(T))) T();
33 }
34 
35 template <typename T>
36 void
AuthDelete(T * ptr)37 AuthDelete(T *ptr)
38 {
39   ptr->~T();
40   TSfree(ptr);
41 }
42 
43 struct HttpIoBuffer {
44   TSIOBuffer buffer;
45   TSIOBufferReader reader;
46 
HttpIoBufferHttpIoBuffer47   explicit HttpIoBuffer(TSIOBufferSizeIndex size = TS_IOBUFFER_SIZE_INDEX_32K)
48     : buffer(TSIOBufferSizedCreate(size)), reader(TSIOBufferReaderAlloc(buffer))
49   {
50   }
51 
~HttpIoBufferHttpIoBuffer52   ~HttpIoBuffer()
53   {
54     TSIOBufferReaderFree(this->reader);
55     TSIOBufferDestroy(this->buffer);
56   }
57 
58   void
resetHttpIoBuffer59   reset(TSIOBufferSizeIndex size = TS_IOBUFFER_SIZE_INDEX_32K)
60   {
61     TSIOBufferReaderFree(this->reader);
62     TSIOBufferDestroy(this->buffer);
63     this->buffer = TSIOBufferSizedCreate(size);
64     this->reader = TSIOBufferReaderAlloc(this->buffer);
65   }
66 
67   void
consumeHttpIoBuffer68   consume(size_t nbytes)
69   {
70     TSIOBufferReaderConsume(this->reader, nbytes);
71   }
72 
73   // noncopyable
74   HttpIoBuffer(const HttpIoBuffer &) = delete;            // delete
75   HttpIoBuffer &operator=(const HttpIoBuffer &) = delete; // delete
76 };
77 
78 struct HttpHeader {
HttpHeaderHttpHeader79   HttpHeader() : buffer(TSMBufferCreate()), header(TSHttpHdrCreate(buffer)) {}
~HttpHeaderHttpHeader80   ~HttpHeader()
81   {
82     TSHttpHdrDestroy(this->buffer, this->header);
83 
84     TSHandleMLocRelease(this->buffer, TS_NULL_MLOC, this->header);
85     TSMBufferDestroy(this->buffer);
86   }
87 
88   TSMBuffer buffer;
89   TSMLoc header;
90 
91   // noncopyable
92   HttpHeader(const HttpHeader &) = delete;            // delete
93   HttpHeader &operator=(const HttpHeader &) = delete; // delete
94 };
95 
96 // Return true if the given HTTP header specified chunked transfer encoding.
97 bool HttpIsChunkedEncoding(TSMBuffer mbuf, TSMLoc mhdr);
98 
99 // Return the value of the Content-Length header.
100 unsigned HttpGetContentLength(TSMBuffer mbuf, TSMLoc mhdr);
101 
102 // Set the value of an arbitrary HTTP header.
103 void HttpSetMimeHeader(TSMBuffer mbuf, TSMLoc mhdr, const char *name, const char *value);
104 void HttpSetMimeHeader(TSMBuffer mbuf, TSMLoc mhdr, const char *name, unsigned value);
105 
106 // Dump the given HTTP header to the debug log.
107 void HttpDebugHeader(TSMBuffer mbuf, TSMLoc mhdr);
108 
109 // vim: set ts=4 sw=4 et :
110