-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault.ashx.cs
200 lines (176 loc) · 6.71 KB
/
default.ashx.cs
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
using System;
using System.Data;
using System.Net;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Threading;
using System.Globalization;
using IT.Services.Text;
namespace ToText
{
/// <summary>
/// Summary description for $codebehindclassname$
/// </summary>
[WebService(Namespace = "http://www.vasiliadis.eu/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class _default : IHttpHandler
{
protected delegate void ProcessMethod(HttpContext context);
public bool IsReusable { get { return false; } }
protected static Dictionary<string, ProcessMethod> methods = new Dictionary<string, ProcessMethod>();
protected bool VerifyApiKey(HttpContext context)
{
string apikey = context.Request["key"];
if (string.IsNullOrEmpty(apikey)) return AccessDenied(context);
//TODO: Add Api Key Verification Code
return true;
}
public void ProcessRequest(HttpContext context)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
string httpMethod = context.Request.HttpMethod.ToUpperInvariant();
if (methods.Count == 0)
{
methods.Add(WebRequestMethods.Http.Get, Process_GET);
}
if (methods.ContainsKey(httpMethod))
methods[httpMethod].Invoke(context);
else
DummyProcess(context);
}
protected void DummyProcess(HttpContext context)
{
if (!VerifyApiKey(context)) return;
context.Response.ContentType = "application/json";
context.Response.StatusCode = (int)HttpStatusCode.NotAcceptable; //406;
context.Response.StatusDescription = "Not Acceptable";
}
protected bool AccessDenied(HttpContext context)
{
context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
context.Response.StatusDescription = "Forbidden";
return false;
}
protected void Process_GET(HttpContext context)
{
if (!VerifyApiKey(context)) return;
string format = context.Request["format"];
if (string.IsNullOrEmpty(format))
format = "json";
else
format = format.ToLower();
string value = context.Request["value"];
if (string.IsNullOrEmpty(value))
{
context.Response.StatusCode = (int)HttpStatusCode.NoContent;
//context.Response.AddHeader("Access-Control-Allow-Origin", "http://www.vasiliadis.eu");
return;
}
if (!Written.ValidValueFormat(value))
{
context.Response.StatusCode = (int)HttpStatusCode.NotAcceptable; //406;
context.Response.StatusDescription = "Not Acceptable";
return;
}
SexForm sexform = SexForm.Neutral;
{
string strform = context.Request["sex"];
if (string.IsNullOrEmpty(strform))
sexform = SexForm.Neutral;
else
{
strform = strform.Trim().ToLower();
switch (strform)
{
case "male":
case "0":
sexform = SexForm.Male;
break;
case "female":
case "1":
sexform = SexForm.Female;
break;
case "neutral":
case "2":
default:
sexform = SexForm.Neutral;
break;
}
}
}
value = value.Trim();
double dblvalue = 0;
bool ok = false;
try
{
ok = double.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture.NumberFormat, out dblvalue);
}
catch (Exception e)
{
System.Diagnostics.Debug.Assert(false, e.Message);
ok = false;
}
if (!ok)
{
DummyProcess(context);
return;
}
dblvalue = Math.Round(dblvalue, 2);
//string retvalue = IT.Services.Text.Written.GetText(dblvalue, SexForm.Neutral);
string[] names = null;
bool showCurrency = false;
if (!string.IsNullOrEmpty(context.Request["names"]))
{
showCurrency = true;
names = context.Request["names"].Split(",".ToCharArray());
for (int i = names.Length; i < 4; i++) { names[i] = names[i - 1]; }
}
//string retvalue = Written.GetText(value, sexform, true, showCurrency);
string retvalue = Written.GetText(value, sexform, true, true);
System.Text.StringBuilder str = new System.Text.StringBuilder(retvalue);
if (showCurrency)
{
retvalue = str
.Replace("€€", names[1])
.Replace("¢¢", names[3])
.Replace("€", names[0])
.Replace("¢", names[2])
.ToString();
}
else
{
retvalue = str
.Replace(" €€", string.Empty)
.Replace(" ¢¢", string.Empty)
.Replace(" €", string.Empty)
.Replace(" ¢", string.Empty)
.ToString();
}
context.Response.AddHeader("Access-Control-Allow-Origin", "http://www.vasiliadis.eu");
if ((format == "json") || string.IsNullOrEmpty(format))
{
context.Response.ContentType = "application/json";
string json = string.Format("{{\"text\":\"{0}\"}}", retvalue);
context.Response.Write(json);
}
else if (format == "text")
{
context.Response.ContentType = "text/plain";
context.Response.Write(retvalue);
}
else if (format == "xml")
{
context.Response.ContentType = "application/xml";
string xml = string.Format("<?xml version=\"1.0\" encoding=\"UTF-8\"?><result><text>{0}</text></result>", retvalue);
context.Response.Write(xml);
}
else
{
DummyProcess(context);
}
}
}
}