HeadersHelper.cs
2.47 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.ServiceModel.Channels;
using System.Text;
using Microsoft.AspNetCore.Http;
namespace SoapCore
{
internal static class HeadersHelper
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string GetSoapAction(HttpContext httpContext, Message requestMessage, System.Xml.XmlDictionaryReader reader)
{
var soapAction = httpContext.Request.Headers["SOAPAction"].FirstOrDefault();
if (soapAction == "\"\"")
{
soapAction = string.Empty;
}
if (string.IsNullOrEmpty(soapAction))
{
foreach (var headerItem in httpContext.Request.Headers["Content-Type"])
{
// I want to avoid allocation as possible as I can(I hope to use Span<T> or Utf8String)
// soap1.2: action name is in Content-Type(like 'action="[action url]"') or body
int i = 0;
// skip whitespace
while (i < headerItem.Length && headerItem[i] == ' ')
{
i++;
}
if (headerItem.Length - i < 6)
{
continue;
}
// find 'action'
if (headerItem[i + 0] == 'a'
&& headerItem[i + 1] == 'c'
&& headerItem[i + 2] == 't'
&& headerItem[i + 3] == 'i'
&& headerItem[i + 4] == 'o'
&& headerItem[i + 5] == 'n')
{
i += 6;
// skip white space
while (i < headerItem.Length && headerItem[i] == ' ')
{
i++;
}
if (headerItem[i] == '=')
{
i++;
// skip whitespace
while (i < headerItem.Length && headerItem[i] == ' ')
{
i++;
}
// action value should be surrounded by '"'
if (headerItem[i] == '"')
{
i++;
int offset = i;
while (i < headerItem.Length && headerItem[i] != '"')
{
i++;
}
if (i < headerItem.Length && headerItem[i] == '"')
{
var charray = headerItem.ToCharArray();
soapAction = new string(charray, offset, i - offset);
break;
}
}
}
}
}
if (string.IsNullOrEmpty(soapAction))
{
soapAction = reader.LocalName;
}
}
if (soapAction.Contains('/'))
{
// soapAction may be a path. Therefore must take the action from the path provided.
soapAction = soapAction.Split('/').Last();
}
if (!string.IsNullOrEmpty(soapAction))
{
// soapAction may have '"' in some cases.
soapAction = soapAction.Trim('"');
}
return soapAction;
}
}
}