fix log to reflect reality
[zxing.git] / csharp / client / result / URIParsedResult.cs
1 /*\r
2 * Copyright 2007 ZXing authors\r
3 *\r
4 * Licensed under the Apache License, Version 2.0 (the "License");\r
5 * you may not use this file except in compliance with the License.\r
6 * You may obtain a copy of the License at\r
7 *\r
8 *      http://www.apache.org/licenses/LICENSE-2.0\r
9 *\r
10 * Unless required by applicable law or agreed to in writing, software\r
11 * distributed under the License is distributed on an "AS IS" BASIS,\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13 * See the License for the specific language governing permissions and\r
14 * limitations under the License.\r
15 */\r
16 using System;\r
17 namespace com.google.zxing.client.result\r
18 {\r
19         \r
20         /// <author>  Sean Owen\r
21         /// </author>\r
22         /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
23         /// </author>\r
24         public sealed class URIParsedResult:ParsedResult\r
25         {\r
26                 public System.String URI\r
27                 {\r
28                         get\r
29                         {\r
30                                 return uri;\r
31                         }\r
32                         \r
33                 }\r
34                 public System.String Title\r
35                 {\r
36                         get\r
37                         {\r
38                                 return title;\r
39                         }\r
40                         \r
41                 }\r
42                 /// <returns> true if the URI contains suspicious patterns that may suggest it intends to\r
43                 /// mislead the user about its true nature. At the moment this looks for the presence\r
44                 /// of user/password syntax in the host/authority portion of a URI which may be used\r
45                 /// in attempts to make the URI's host appear to be other than it is. Example:\r
46                 /// http://yourbank.com@phisher.com  This URI connects to phisher.com but may appear\r
47                 /// to connect to yourbank.com at first glance.\r
48                 /// </returns>\r
49                 public bool PossiblyMaliciousURI\r
50                 {\r
51                         get\r
52                         {\r
53                                 return containsUser();\r
54                         }\r
55                         \r
56                 }\r
57                 override public System.String DisplayResult\r
58                 {\r
59                         get\r
60                         {\r
61                                 System.Text.StringBuilder result = new System.Text.StringBuilder(30);\r
62                                 maybeAppend(title, result);\r
63                                 maybeAppend(uri, result);\r
64                                 return result.ToString();\r
65                         }\r
66                         \r
67                 }\r
68                 \r
69                 //UPGRADE_NOTE: Final was removed from the declaration of 'uri '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
70                 private System.String uri;\r
71                 //UPGRADE_NOTE: Final was removed from the declaration of 'title '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
72                 private System.String title;\r
73                 \r
74                 public URIParsedResult(System.String uri, System.String title):base(ParsedResultType.URI)\r
75                 {\r
76                         this.uri = massageURI(uri);\r
77                         this.title = title;\r
78                 }\r
79                 \r
80                 private bool containsUser()\r
81                 {\r
82                         // This method is likely not 100% RFC compliant yet\r
83                         int hostStart = uri.IndexOf(':'); // we should always have scheme at this point\r
84                         hostStart++;\r
85                         // Skip slashes preceding host\r
86                         int uriLength = uri.Length;\r
87                         while (hostStart < uriLength && uri[hostStart] == '/')\r
88                         {\r
89                                 hostStart++;\r
90                         }\r
91                         //UPGRADE_WARNING: Method 'java.lang.String.indexOf' was converted to 'System.String.IndexOf' which may throw an exception. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1101'"\r
92                         int hostEnd = uri.IndexOf('/', hostStart);\r
93                         if (hostEnd < 0)\r
94                         {\r
95                                 hostEnd = uriLength;\r
96                         }\r
97                         //UPGRADE_WARNING: Method 'java.lang.String.indexOf' was converted to 'System.String.IndexOf' which may throw an exception. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1101'"\r
98                         int at = uri.IndexOf('@', hostStart);\r
99                         return at >= hostStart && at < hostEnd;\r
100                 }\r
101                 \r
102                 /// <summary> Transforms a string that represents a URI into something more proper, by adding or canonicalizing\r
103                 /// the protocol.\r
104                 /// </summary>\r
105                 private static System.String massageURI(System.String uri)\r
106                 {\r
107                         int protocolEnd = uri.IndexOf(':');\r
108                         if (protocolEnd < 0)\r
109                         {\r
110                                 // No protocol, assume http\r
111                                 uri = "http://" + uri;\r
112                         }\r
113                         else if (isColonFollowedByPortNumber(uri, protocolEnd))\r
114                         {\r
115                                 // Found a colon, but it looks like it is after the host, so the protocol is still missing\r
116                                 uri = "http://" + uri;\r
117                         }\r
118                         else\r
119                         {\r
120                                 // Lowercase protocol to avoid problems\r
121                                 uri = uri.Substring(0, (protocolEnd) - (0)).ToLower() + uri.Substring(protocolEnd);\r
122                         }\r
123                         return uri;\r
124                 }\r
125                 \r
126                 private static bool isColonFollowedByPortNumber(System.String uri, int protocolEnd)\r
127                 {\r
128                         //UPGRADE_WARNING: Method 'java.lang.String.indexOf' was converted to 'System.String.IndexOf' which may throw an exception. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1101'"\r
129                         int nextSlash = uri.IndexOf('/', protocolEnd + 1);\r
130                         if (nextSlash < 0)\r
131                         {\r
132                                 nextSlash = uri.Length;\r
133                         }\r
134                         if (nextSlash <= protocolEnd + 1)\r
135                         {\r
136                                 return false;\r
137                         }\r
138                         for (int x = protocolEnd + 1; x < nextSlash; x++)\r
139                         {\r
140                                 if (uri[x] < '0' || uri[x] > '9')\r
141                                 {\r
142                                         return false;\r
143                                 }\r
144                         }\r
145                         return true;\r
146                 }\r
147         }\r
148 }