ASP.NET - What Characters does Server.HtmlEncode Encode into Named Character Entities - asp.net

What characters does Server.HtmlEncode encode into named character entities?
So far I have only found < > & and " surely there must be more than this?

This is the code of HtmlEncode, so here you can see how they done it.
public static unsafe void HtmlEncode(string value, TextWriter output)
{
if (value != null)
{
if (output == null)
{
throw new ArgumentNullException("output");
}
int num = IndexOfHtmlEncodingChars(value, 0);
if (num == -1)
{
output.Write(value);
}
else
{
int num2 = value.Length - num;
fixed (char* str = ((char*) value))
{
char* chPtr = str;
char* chPtr2 = chPtr;
while (num-- > 0)
{
chPtr2++;
output.Write(chPtr2[0]);
}
while (num2-- > 0)
{
chPtr2++;
char ch = chPtr2[0];
if (ch <= '>')
{
switch (ch)
{
case '&':
{
output.Write("&");
continue;
}
case '\'':
{
output.Write("'");
continue;
}
case '"':
{
output.Write(""");
continue;
}
case '<':
{
output.Write("<");
continue;
}
case '>':
{
output.Write(">");
continue;
}
}
output.Write(ch);
continue;
}
if ((ch >= '\x00a0') && (ch < 'Ā'))
{
output.Write("&#");
output.Write(ch.ToString(NumberFormatInfo.InvariantInfo));
output.Write(';');
}
else
{
output.Write(ch);
}
}
}
}
}
}

.NET 4 and 4.5 encode single quotes also, which doesn't appear to be in the answer

Related

Why Huge Data Are not inserted or updated correctly from MVC5 to sql Server

I made a project in MVC5 using code first approach.
All of my project work correctly but a little problem occurred some times I have a table in sql server which has several columns with one Boolean column when I insert or update multiple rows (means the rows are affected more than one hundred) of this tableو The data of all the columns are inserted or updated correctly, but the data of the Boolean type column is not stored correctly, as this column should normally be stored as 0, but when the number of records increases, this column sometimes becomes 1 of some rows randomly means not a specific row at all time.
when I insert or update using debug mode the problem not occurred but when i run without debug mode or in hosting environment the arise some times.
--This is the insert or update method code--
public bool InsertUpdateDelete(IEnumerable<TEntity> modelObjects, IEnumerable<TEntity> dbObjects,List<int> lstDeleted=null)
{
try
{
foreach (var modelObject in modelObjects)
{
List<PropertyInfo> properties = modelObject.GetType().GetProperties().ToList();
var pmId = properties.Where(x => x.Name == "Id").FirstOrDefault();
var val = Convert.ToInt64(pmId.GetValue(modelObject));
if (val == 0 && !Misc.Empty(modelObject))
{
Insert(modelObject);
}
else if (val != 0)
{
TEntity dbObject = null;
dbObjects.ToList().ForEach(x =>
{
if (Convert.ToDouble(x.GetType().GetProperty(pmId.Name).GetValue(x)) == val)
{
dbObject = x;
}
});
if (dbObject != null)
{
if (Misc.Empty(modelObject))
{
var pDel = dbObject.GetType().GetProperty("IsDeleted");
pDel.SetValue(dbObject, true);
Update(dbObject);
}
else
{
var newObject = (TEntity)Misc.ReverseDataObjects(modelObject, dbObject);
Update(newObject);
}
}
}
}
if (lstDeleted != null) {
foreach (int id in lstDeleted) {
TEntity dbObject = null;
dbObjects.ToList().ForEach(x =>
{
if (Convert.ToDouble(x.GetType().GetProperty("Id").GetValue(x)) == id)
{
dbObject = x;
}
});
if (dbObject != null)
{
var pDel = dbObject.GetType().GetProperty("IsDeleted");
pDel.SetValue(dbObject, true);
Update(dbObject);
}
}
}
return true;
}
catch (Exception e)
{
return false;
}
}
---the following method check the empty rows--
public static bool EmptyRow(object obj)
{
bool IsEmpty = true;
try
{
List<PropertyInfo> properties = obj.GetType().GetProperties().ToList();
if (properties.Count() > 0)
{
foreach (PropertyInfo property in properties)
{
if (property.Name != "UserId" &&
property.Name != "EntryDate" &&
property.Name != "Id" &&
property.Name != "SchoolId" &&
property.Name != "DataYear" &&
property.Name != "ClassTypeId" &&
property.Name != "GenderId" &&
property.Name != "BuildingId" &&
property.Name != "Age" &&
property.Name != "IsDeleted" &&
)
{
if ((typeof(int) == property.PropertyType || property.PropertyType == typeof(Nullable<Int32>) || property.PropertyType == typeof(Int32)) && property.GetValue(obj) != null)
{
var val = int.Parse(property.GetValue(obj).ToString());
if (val != 0)
{
IsEmpty = false;
break;
}
}
else if ((property.PropertyType == typeof(String) || typeof(string) == property.GetType()) && property.GetValue(obj) != null)
{
var val = property.GetValue(obj).ToString();
if (val != null && val != string.Empty)
{
IsEmpty = false;
break;
}
}
else if ((property.PropertyType == typeof(double) || property.PropertyType == typeof(Nullable<Int64>) || property.PropertyType == typeof(Int64)) && property.GetValue(obj) != null)
{
var val = Int64.Parse(property.GetValue(obj).ToString());
if (val != 0)
{
IsEmpty = false;
break;
}
}
}
}
}
return IsEmpty;
}
catch (Exception e)
{
ErrorLog("Misc", "IsEmptyRowData", e, null);
}
return IsEmpty;
}

Wso2 Stream Processor : Error occurred while processing eventByteBufferQueue

I have two nodes of wso2-am analytics server (2.6.0) which is Wso2 Stream processors. I see following error on passive node of cluster. The active node is fine and I don't see any error. Analytics result has no impact for users who is viewing data on API Publisher or Store. however there is an error in passive node.
please advise what is causing following issue..
2019-02-26 17:06:09,513] ERROR {org.wso2.carbon.stream.processor.core.ha.tcp.EventSyncServer} - Error occurred while processing eventByteBufferQueue null java.nio.BufferUnderflowException
Just meet the same issue, here is my problem and solution.
1) Using the WSO2 SP HA deployment.
2) When Event come in active node and according the source mapping of the streaming, some fields are NULL
3) Active Node would like sync this event to passive node
4) passive node pick up the event data from the 'eventByteBufferQueue' to meet the standby-take over mechanism
5) passive node cannot parse the data from active node and reports error exception.
the root cause is SP only support NULL String by default, when NULL with LONG, INTEGER.. the error occurred. but for me, Long fields have NULL is the normal case, you can change data type to string.
here is my solution:
org.wso2.carbon.stream.processor.core_2.0.478.jar
Add logic to support NULL
BinaryMessageConverterUtil.java for sending event data from active node
public final class BinaryMessageConverterUtil {
public static int getSize(Object data) {
if (data instanceof String) {
return 4 + ((String) data).length();
} else if (data instanceof Integer) {
return 4;
} else if (data instanceof Long) {
return 8;
} else if (data instanceof Float) {
return 4;
} else if (data instanceof Double) {
return 8;
} else if (data instanceof Boolean) {
return 1;
} else if (data == null) {
return 0;
}else {
//TODO
return 4;
}
}
public static EventDataMetaInfo getEventMetaInfo(Object data) {
int eventSize;
Attribute.Type attributeType;
if (data instanceof String) {
attributeType = Attribute.Type.STRING;
eventSize = 4 + ((String) data).length();
} else if (data instanceof Integer) {
attributeType = Attribute.Type.INT;
eventSize = 4;
} else if (data instanceof Long) {
attributeType = Attribute.Type.LONG;
eventSize = 8;
} else if (data instanceof Float) {
attributeType = Attribute.Type.FLOAT;
eventSize = 4;
} else if (data instanceof Double) {
attributeType = Attribute.Type.DOUBLE;
eventSize = 8;
} else if (data instanceof Boolean) {
attributeType = Attribute.Type.BOOL;
eventSize = 1;
} else if (data == null){
attributeType = Attribute.Type.OBJECT;
eventSize = 0; //'no content between the HA nodes for NULL fields'
} else {
//TODO
attributeType = Attribute.Type.OBJECT;
eventSize = 1;
}
return new EventDataMetaInfo(eventSize, attributeType);
}
public static void assignData(Object data, ByteBuffer eventDataBuffer) throws IOException {
if (data instanceof String) {
eventDataBuffer.putInt(((String) data).length());
eventDataBuffer.put((((String) data).getBytes(Charset.defaultCharset())));
} else if (data instanceof Integer) {
eventDataBuffer.putInt((Integer) data);
} else if (data instanceof Long) {
eventDataBuffer.putLong((Long) data);
} else if (data instanceof Float) {
eventDataBuffer.putFloat((Float) data);
} else if (data instanceof Double) {
eventDataBuffer.putDouble((Double) data);
} else if (data instanceof Boolean) {
eventDataBuffer.put((byte) (((Boolean) data) ? 1 : 0));
} else if (data == null){
//put nothing into he Buffer
} else {
eventDataBuffer.putInt(0);
}
}
public static String getString(ByteBuf byteBuf, int size) throws UnsupportedEncodingException {
byte[] bytes = new byte[size];
byteBuf.readBytes(bytes);
return new String(bytes, Charset.defaultCharset());
}
public static String getString(ByteBuffer byteBuf, int size) throws UnsupportedEncodingException {
byte[] bytes = new byte[size];
byteBuf.get(bytes);
return new String(bytes, Charset.defaultCharset());
}
}
SiddhiEventConverter.java for processing event data at passive node
static Object[] toObjectArray(ByteBuffer byteBuffer,
String[] attributeTypeOrder) throws UnsupportedEncodingException {
if (attributeTypeOrder != null) {
Object[] objects = new Object[attributeTypeOrder.length];
for (int i = 0; i < attributeTypeOrder.length; i++) {
switch (attributeTypeOrder[i]) {
case "INT":
objects[i] = byteBuffer.getInt();
break;
case "LONG":
objects[i] = byteBuffer.getLong();
break;
case "STRING":
int stringSize = byteBuffer.getInt();
if (stringSize == 0) {
objects[i] = null;
} else {
objects[i] = BinaryMessageConverterUtil.getString(byteBuffer, stringSize);
}
break;
case "DOUBLE":
objects[i] = byteBuffer.getDouble();
break;
case "FLOAT":
objects[i] = byteBuffer.getFloat();
break;
case "BOOL":
objects[i] = byteBuffer.get() == 1;
break;
case "OBJECT":
//for NULL fields
objects[i] = null;
break;
default:
// will not occur
}
}
return objects;
} else {
return null;
}
}

QEventDispatcherWin32::processEvents Logical issues

When see the qt source code(QEventDispatcherWin32::processEvents),i find a Logical issues.code as below,first it writes if(!haveMessage) next if(haveMessage),so code else if (...) and else{...} will never execute,what i think is right?
code as:
if (!haveMessage) {
// no message - check for signalled objects
for (int i = 0; i < (int)nCount; i++)
pHandles[i] = d->winEventNotifierList.at(i)->handle();
waitRet = MsgWaitForMultipleObjectsEx(nCount, pHandles, 0, QS_ALLINPUT, MWMO_ALERTABLE);
if ((haveMessage = (waitRet == WAIT_OBJECT_0 + nCount))) {
// a new message has arrived, process it
continue;
}
}
if (haveMessage) {
#ifdef Q_OS_WINCE
// WinCE doesn't support hooks at all, so we have to call this by hand :(
(void) qt_GetMessageHook(0, PM_REMOVE, (LPARAM)&msg);
#endif
if (d->internalHwnd == msg.hwnd && msg.message == WM_QT_SENDPOSTEDEVENTS) {
if (seenWM_QT_SENDPOSTEDEVENTS) {
// when calling processEvents() "manually", we only want to send posted
// events once
needWM_QT_SENDPOSTEDEVENTS = true;
continue;
}
seenWM_QT_SENDPOSTEDEVENTS = true;
}
else if (msg.message == WM_TIMER) {
// avoid live-lock by keeping track of the timers we've already sent
bool found = false;
for (int i = 0; !found && i < processedTimers.count(); ++i) {
const MSG processed = processedTimers.constData()[i];
found = (processed.wParam == msg.wParam && processed.hwnd == msg.hwnd && processed.lParam == msg.lParam);
}
if (found)
continue;
processedTimers.append(msg);
}
else if (msg.message == WM_QUIT) {
if (QCoreApplication::instance())
QCoreApplication::instance()->quit();
return false;
}
if (!filterEvent(&msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else if (waitRet >= WAIT_OBJECT_0 && waitRet < WAIT_OBJECT_0 + nCount) {
d->activateEventNotifier(d->winEventNotifierList.at(waitRet - WAIT_OBJECT_0));
}
else {
// nothing todo so break
break;
}

How to Explore Controls whit For in ASP.Net

First Excuse my English is bad!!!
I have A Web Form Where there are 120 RadioButtonList .I should monitored them
if (RadioButtonList1.SelectedValue == "1")
{
Response.Write("1");
}
else
{
Response.Write("0");
}
if (RadioButtonList2.SelectedValue == "1")
{
Response.Write("1");
}
else
{
Response.Write("0");
}
if (RadioButtonList3.SelectedValue == "1")
{
Response.Write("1");
}
else
{
Response.Write("0");
}
.
.
.
if (RadioButtonList120.SelectedValue == "1")
{
Response.Write("1");
}
else
{
Response.Write("0");
}
now i want do this whit For loop
Like this:
for (int i = 1; i <= 120; i++)
{
if (RadioButtonList[i].SelectedValue == "1")//Like Array :))
{
Response.Write("1");
}
else
{
Response.Write("0");
}
}
(This example is stupid.)
How did it do ?
again Excuse my English is bad!!!
string id="radiobuttonlist";
for(int i=1;i<121;i++)
{
id=id+i.ToString();
RadioButtonList rbl = (RadioButtonList)Page.FindControl(id);
if(rbl.SelectedValue == 1)
{
//do something
}
else
{
//do something
}
}

regular expression asp.net

I trying to improve a textbox that filters a gridview, by implementing an enhanced textbox that can identify AND, OR, NOT operators by searching for this keywords in the string the user inputs in the textbox.
I am trying to do a regular expression to group the results but I'm not very good at this and I am failing to get what I want.
An example of what I want is as follows:
string = "build1 and build2 and build3 or build4 or not build5 and not build6"
results in a split mode:
build1 and
build2 and
build3 or
build4 or not
build5 and not
build6
This is because then I will take the first for example and replace with
SomeTable.Name_Of_Build = 'build1' AND
SomeTable.Name_Of_Build = 'build2' AND .... so on
This works for me
\w+(\sand\snot|\sor\snot|\sand|\sor|$)
I might recommend that instead of using a regular expression to group the results you do something like this. I think it would be more robust than trying to guess at the right regex.
string filter = "build1 and buil2 and build3 or build4 or not build5"
list<string> filterTokens = filter.Split(new char[] {' '})
string gridViewFilter = "";
bool notEqual = false;
foreach(string token in filterTokens)
{
if(token == "and")
{
gridViewFilter += "and"
}
else if(token == "or")
{
gridViewFilter += "or"
}
else if(token == "not")
{
notEqual = true;
}
else if(notEqual)
{
gridViewFilter += "SomeTable.Name_Of_Build <> '" + token + "'";
notEqual = false;
}
else
{
gridViewFilter += "SomeTable.Name_Of_Build <> '" + token + "'";
}
}
Also, if you really want to implement a robust and full featured sort you need to look into using Reverse Polish Notation (RPN). It would allow you to handle parentheses and order of operations. An RPN implementation would look something like this.
private bool CheckForFilterMatch(string filter, List<string> values, bool exactMatch)
{
for (int i = 0; i < values.Count; i++)
{
values[i] = values[i].ToLower();
}
if (filter.Trim() == "")
{
return true;
}
List<string> rpn = GetPostFixNotation(filter);
Stack<bool> output = new Stack<bool>();
foreach (string token in rpn)
{
if (IsValue(token))
{
bool isMatch;
if (exactMatch)
{
isMatch = values.Contains(token.ToLower());
}
else
{
isMatch = false;
foreach (string value in values)
{
isMatch = (value.IndexOf(token.ToLower()) != -1);
if (isMatch) break;
}
}
output.Push(isMatch);
}
else if (IsOperator(token))
{
bool operand1 = output.Pop();
bool operand2 = output.Pop();
if (token == "&")
{
output.Push(operand1 && operand2);
}
if (token == "|")
{
output.Push(operand1 || operand2);
}
}
}
return output.Pop();
}
public List<string> GetPostFixNotation(string filter)
{
if (filter == "")
{
return new List<string>();
}
List<string> postFixNotation = new List<string>();
Queue<string> output = new Queue<string>();
Stack<string> operators = new Stack<string>();
List<string> parsedFilter = ParseFilterTokens(filter);
foreach (string token in parsedFilter)
{
if (IsValue(token))
{
output.Enqueue(token);
}
else if (IsOperatorNoParenth(token))
{
while (operators.Count > 0 && IsOperatorNoParenth(operators.Peek()))
{
if ((operators.Count > 0 && (Precedence(token) <= Precedence(operators.Peek()))))
{
string operatorToReturn = operators.Pop();
output.Enqueue(operatorToReturn);
}
else break;
}
operators.Push(token);
}
else if (token == "(")
{
operators.Push(token);
}
else if (token == ")")
{
while (operators.Count > 0 && operators.Peek() != "(")
{
output.Enqueue(operators.Pop());
}
operators.Pop();
}
}
while (operators.Count > 0)
{
output.Enqueue(operators.Pop());
}
while (output.Count > 0)
{
postFixNotation.Add(output.Dequeue());
}
return postFixNotation;
}
Try this regex out:
(build[^b]+)

Resources