Skip to content

Commit 30e05e7

Browse files
committed
new switches to set the HTTP Content-Type header
1 parent 000a706 commit 30e05e7

File tree

10 files changed

+42
-8
lines changed

10 files changed

+42
-8
lines changed

Routing.cxx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2249,6 +2249,7 @@ void HttpPolicy::LoadConfig(const PString & instance)
22492249
m_url = GkConfig()->GetString(m_iniSection, "URL", "");
22502250
m_body = GkConfig()->GetString(m_iniSection, "Body", "");
22512251
m_method = GkConfig()->GetString(m_iniSection, "Method", "POST");
2252+
m_contentType = GkConfig()->GetString(m_iniSection, "ContentType", "text/plain");
22522253
PString resultRegex = GkConfig()->GetString(m_iniSection, "ResultRegex", ".*"); // match everything
22532254
m_resultRegex = PRegularExpression(resultRegex, PRegularExpression::Extended);
22542255
m_resultRegex = PRegularExpression(resultRegex);
@@ -2342,7 +2343,8 @@ void HttpPolicy::RunPolicy(
23422343
url = parts[0];
23432344
body = parts[1];
23442345
} else {
2345-
headerlist = curl_slist_append(headerlist, "Content-Type: text/plain");
2346+
PString header = PString("Content-Type: ") + m_contentType;
2347+
headerlist = curl_slist_append(headerlist, (const char *)header);
23462348
(void)curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
23472349
}
23482350
(void)curl_easy_setopt(curl, CURLOPT_POSTFIELDS, (const char *)body);
@@ -2379,7 +2381,7 @@ void HttpPolicy::RunPolicy(
23792381
body = parts[1];
23802382
}
23812383
PMIMEInfo outMIME;
2382-
outMIME.SetAt(PMIMEInfo::ContentTypeTag(), "text/plain");
2384+
outMIME.SetAt(PMIMEInfo::ContentTypeTag(), (const char *)m_contentType);
23832385
PMIMEInfo replyMIME;
23842386
if (!http.PostData(url, outMIME, body, replyMIME, result)) {
23852387
PTRACE(2, m_name << "\tCould not POST to " << host);

Routing.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ class HttpPolicy : public DynamicPolicy {
601601
PString m_url;
602602
PString m_body;
603603
PCaselessString m_method;
604+
PString m_contentType;
604605
PRegularExpression m_resultRegex;
605606
PRegularExpression m_deleteRegex;
606607
PRegularExpression m_errorRegex;

changes.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
Changes from 5.7 to 5.8
22
=======================
3+
- new switches to set the HTTP Content-Type header: [HttpAcct] ContentType=, [HttpPasswordAuth] ContentType=
4+
and [Routing::Http] ContentType=
35
- new switch [Proxy] AllowAnyRTPSourcePortForH239From= to handle incorrect RTCP addresses in H.239 OLC (EdgeProtect interop)
46
- new switch [RoutedMode] MatchH239SessionsByIDOnly= to never attempt to match a H.239 reverse channel by type
57
for improved interoperability with EdgeProtect

docs/manual/acct.sgml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,11 @@ Default: <tt>N/A</tt>
10571057
<p>
10581058
HTTP method to use. Currently GET and POST are supported.
10591059

1060+
<item><tt>ContentType=application/json</tt><newline>
1061+
Default: <tt>text/plain</tt><newline>
1062+
<p>
1063+
The content type header to send in POST requests.
1064+
10601065
<item><tt/TimestampFormat=MySQL/<newline>
10611066
Default: <tt>N/A</tt><newline>
10621067
<p>

docs/manual/auth.sgml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,6 +1815,11 @@ Default: <tt>POST</tt>
18151815
<p>
18161816
HTTP method to use. Currently GET and POST are supported.
18171817

1818+
<item><tt>ContentType=application/json</tt><newline>
1819+
Default: <tt>text/plain</tt><newline>
1820+
<p>
1821+
The content type header to send in POST requests.
1822+
18181823
<item><tt/ResultRegex=[0-9]+/<newline>
18191824
Default: <tt>.*</tt>
18201825
<p>

docs/manual/routing.sgml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,11 @@ Default: <tt>POST</tt>
641641
<p>
642642
HTTP method to use. Currently GET and POST are supported.
643643

644+
<item><tt>ContentType=application/json</tt><newline>
645+
Default: <tt>text/plain</tt><newline>
646+
<p>
647+
The content type header to send in POST requests.
648+
644649
<item><tt/ResultRegex=[0-9]+/<newline>
645650
Default: <tt>.*</tt>
646651
<p>

gk.cxx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ const char * KnownConfigEntries[][2] = {
398398
{ "HttpAcct", "AlertURL" },
399399
{ "HttpAcct", "ConnectBody" },
400400
{ "HttpAcct", "ConnectURL" },
401+
{ "HttpAcct", "ContentType" },
401402
{ "HttpAcct", "MediaFailBody" },
402403
{ "HttpAcct", "MediaFailURL" },
403404
{ "HttpAcct", "Method" },
@@ -419,6 +420,7 @@ const char * KnownConfigEntries[][2] = {
419420
{ "HttpAcct", "UnregisterBody" },
420421
{ "HttpAcct", "UnregisterURL" },
421422
{ "HttpPasswordAuth", "Body" },
423+
{ "HttpPasswordAuth", "ContentType" },
422424
{ "HttpPasswordAuth", "DeleteRegex" },
423425
{ "HttpPasswordAuth", "ErrorRegex" },
424426
{ "HttpPasswordAuth", "Method" },
@@ -764,6 +766,7 @@ const char * KnownConfigEntries[][2] = {
764766
{ "Routing::Forwarding", "Username" },
765767
#if defined (P_HTTP) || defined (HAS_LIBCURL)
766768
{ "Routing::Http", "Body" },
769+
{ "Routing::Http", "ContentType" },
767770
{ "Routing::Http", "DeleteRegex" },
768771
{ "Routing::Http", "ErrorRegex" },
769772
#ifdef HAS_JSON

gkauth.cxx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// gkauth.cxx
44
//
5-
// Copyright (c) 2001-2019, Jan Willamowius
5+
// Copyright (c) 2001-2021, Jan Willamowius
66
//
77
// This work is published under the GNU Public License version 2 (GPLv2)
88
// see file COPYING for details.
@@ -2352,6 +2352,7 @@ class HttpPasswordAuth : public SimplePasswordAuth
23522352
PString m_url;
23532353
PString m_body;
23542354
PCaselessString m_method;
2355+
PString m_contentType;
23552356
PRegularExpression m_resultRegex;
23562357
PRegularExpression m_deleteRegex;
23572358
PRegularExpression m_errorRegex;
@@ -2363,6 +2364,7 @@ HttpPasswordAuth::HttpPasswordAuth(const char* authName)
23632364
m_url = GkConfig()->GetString("HttpPasswordAuth", "URL", "");
23642365
m_body = GkConfig()->GetString("HttpPasswordAuth", "Body", "");
23652366
m_method = GkConfig()->GetString("HttpPasswordAuth", "Method", "POST");
2367+
m_contentType = GkConfig()->GetString("HttpPasswordAuth", "ContentType", "text/plain");
23662368
PString resultRegex = GkConfig()->GetString("HttpPasswordAuth", "ResultRegex", ".*"); // match everything
23672369
m_resultRegex = PRegularExpression(resultRegex, PRegularExpression::Extended);
23682370
m_resultRegex = PRegularExpression(resultRegex);
@@ -2429,7 +2431,8 @@ bool HttpPasswordAuth::GetPassword(const PString & alias, PString & password, st
24292431
url = parts[0];
24302432
body = parts[1];
24312433
} else {
2432-
headerlist = curl_slist_append(headerlist, "Content-Type: text/plain");
2434+
PString header = PString("Content-Type: ") + m_contentType;
2435+
headerlist = curl_slist_append(headerlist, (const char *)header);
24332436
(void)curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
24342437
}
24352438
(void)curl_easy_setopt(curl, CURLOPT_POSTFIELDS, (const char *)body);
@@ -2466,7 +2469,7 @@ bool HttpPasswordAuth::GetPassword(const PString & alias, PString & password, st
24662469
body = parts[1];
24672470
}
24682471
PMIMEInfo outMIME;
2469-
outMIME.SetAt(PMIMEInfo::ContentTypeTag(), "text/plain");
2472+
outMIME.SetAt(PMIMEInfo::ContentTypeTag(), (const char *)m_contentType);
24702473
PMIMEInfo replyMIME;
24712474
if (!http.PostData(url, outMIME, body, replyMIME, result)) {
24722475
PTRACE(2, "HttpPasswordAuth\tCould not POST to " << host);

httpacct.cxx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* accounting module for GNU Gatekeeper that sends it's messages over HTTP
55
*
6-
* Copyright (c) 2018, Jan Willamowius
6+
* Copyright (c) 2018-2021, Jan Willamowius
77
*
88
* This work is published under the GNU Public License version 2 (GPLv2)
99
* see file COPYING for details.
@@ -37,6 +37,7 @@ HttpAcct::HttpAcct(const char* moduleName, const char* cfgSecName)
3737
const PString & cfgSec = GetConfigSectionName();
3838
m_timestampFormat = cfg->GetString(cfgSec, "TimestampFormat", "");
3939
m_method = GkConfig()->GetString(cfgSec, "Method", "POST");
40+
m_contentType = cfg->GetString(cfgSec, "ContentType", "text/plain");
4041
m_startURL = cfg->GetString(cfgSec, "StartURL", "");
4142
m_startBody = cfg->GetString(cfgSec, "StartBody", "");
4243
m_stopURL = cfg->GetString(cfgSec, "StopURL", "");
@@ -187,6 +188,7 @@ GkAcctLogger::Status HttpAcct::HttpLog(PString url, PString body)
187188
CURLcode curl_res = CURLE_FAILED_INIT;
188189
CURL * curl = curl_easy_init();
189190
if (curl) {
191+
struct curl_slist *headerlist = NULL;
190192
if (m_method == "GET") {
191193
// nothing special to do
192194
} else if (m_method == "POST") {
@@ -195,6 +197,9 @@ GkAcctLogger::Status HttpAcct::HttpLog(PString url, PString body)
195197
url = parts[0];
196198
body = parts[1];
197199
}
200+
PString header = PString("Content-Type: ") + m_contentType;
201+
headerlist = curl_slist_append(headerlist, (const char *)header);
202+
(void)curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
198203
(void)curl_easy_setopt(curl, CURLOPT_POSTFIELDS, (const char *)body);
199204
} else {
200205
PTRACE(2, "HttpAcct\tUnsupported method " << m_method);
@@ -209,6 +214,7 @@ GkAcctLogger::Status HttpAcct::HttpLog(PString url, PString body)
209214
(void)curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
210215
}
211216
curl_res = curl_easy_perform(curl);
217+
curl_slist_free_all(headerlist);
212218
curl_easy_cleanup(curl);
213219
}
214220

@@ -230,7 +236,7 @@ GkAcctLogger::Status HttpAcct::HttpLog(PString url, PString body)
230236
body = parts[1];
231237
}
232238
PMIMEInfo outMIME;
233-
outMIME.SetAt(PMIMEInfo::ContentTypeTag(), "text/plain");
239+
outMIME.SetAt(PMIMEInfo::ContentTypeTag(), (const char *)m_contentType);
234240
PMIMEInfo replyMIME;
235241
if (!http.PostData(url, outMIME, body, replyMIME, result)) {
236242
PTRACE(2, "HttpAcct\tCould not POST to " << host);

httpacct.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* accounting module for GNU Gatekeeper that sends it's messages over HTTP
55
*
6-
* Copyright (c) 2018, Jan Willamowius
6+
* Copyright (c) 2018-2021, Jan Willamowius
77
*
88
* This work is published under the GNU Public License version 2 (GPLv2)
99
* see file COPYING for details.
@@ -94,6 +94,8 @@ class HttpAcct : public GkAcctLogger
9494
PString m_mediaFailBody;
9595
/// HTTP method: GET or POST
9696
PString m_method;
97+
/// HTTP Content-Type used for POSTs
98+
PString m_contentType;
9799
/// timestamp formatting string
98100
PString m_timestampFormat;
99101
};

0 commit comments

Comments
 (0)