XmlRpcRequest MakeXmlRpcRequest2(HttpRequestMessage message, MethodInfo mi, object[] parameters, object clientObj, string xmlRpcMethod, Guid proxyId) { message.Method = HttpMethod.Post; message.Headers.TryAppendWithoutValidation("Content-Type", "text/xml"); string rpcMethodName = XmlRpcTypeInfo.GetRpcMethodName(mi); XmlRpcRequest req = new XmlRpcRequest(rpcMethodName, parameters, mi, xmlRpcMethod, proxyId); return(req); }
private void CheckImplictString(Type valType, MappingStack mappingStack) { if (valType != null && valType != typeof(string) && !valType.GetTypeInfo().IsEnum) { throw new XmlRpcTypeMismatchException(mappingStack.MappingType + " contains implicit string value where " + XmlRpcTypeInfo.GetXmlRpcTypeString(valType) + " expected " + StackDump(mappingStack)); } }
XmlRpcRequest MakeXmlRpcRequest(WebRequest webReq, MethodInfo mi, object[] parameters, object clientObj, string xmlRpcMethod, Guid proxyId) { webReq.Method = "POST"; webReq.ContentType = "text/xml"; string rpcMethodName = XmlRpcTypeInfo.GetRpcMethodName(mi); XmlRpcRequest req = new XmlRpcRequest(rpcMethodName, parameters, mi, xmlRpcMethod, proxyId); return(req); }
string GetEffectiveUrl(object clientObj) { // client can either have define URI in attribute or have set it // via proxy's ServiceURI property - but must exist by now if (!string.IsNullOrEmpty(Url)) { return(Url); } string useUrl = XmlRpcTypeInfo.GetUrlFromAttribute(clientObj.GetType()); if (!string.IsNullOrEmpty(useUrl)) { return(useUrl); } throw new XmlRpcMissingUrl("Proxy XmlRpcUrl attribute or Url " + "property not set."); }
private void CheckExpectedType(Type expectedType, Type actualType, MappingStack mappingStack) { if (expectedType != null && expectedType.GetTypeInfo().IsEnum) { Type[] i4Types = new Type[] { typeof(byte), typeof(sbyte), typeof(short), typeof(ushort), typeof(int) }; Type[] i8Types = new Type[] { typeof(uint), typeof(long) }; Type underlyingType = Enum.GetUnderlyingType(expectedType); if (Array.IndexOf(i4Types, underlyingType) >= 0) { expectedType = typeof(Int32); } else if (Array.IndexOf(i8Types, underlyingType) >= 0) { expectedType = typeof(long); } else { throw new XmlRpcInvalidEnumValue(mappingStack.MappingType + " contains " + XmlRpcTypeInfo.GetXmlRpcTypeString(actualType) + " which cannot be mapped to " + XmlRpcTypeInfo.GetXmlRpcTypeString(expectedType) + " " + StackDump(mappingStack)); } } // TODO: throw exception for invalid enum type if (expectedType != null && expectedType != typeof(Object) && expectedType != actualType && (actualType.GetTypeInfo().IsValueType && expectedType != typeof(Nullable <>).MakeGenericType(actualType))) { throw new XmlRpcTypeMismatchException(mappingStack.MappingType + " contains " + XmlRpcTypeInfo.GetXmlRpcTypeString(actualType) + " value where " + XmlRpcTypeInfo.GetXmlRpcTypeString(expectedType) + " expected " + StackDump(mappingStack)); } }
private object MapArray(IEnumerator <Node> iter, Type valType, MappingStack mappingStack, MappingAction mappingAction, out Type mappedType) { mappedType = null; // required type must be an array if (valType != null && !(valType.IsArray == true || valType == typeof(Array) || valType == typeof(object))) { throw new XmlRpcTypeMismatchException(mappingStack.MappingType + " contains array value where " + XmlRpcTypeInfo.GetXmlRpcTypeString(valType) + " expected " + StackDump(mappingStack)); } if (valType != null) { XmlRpcType xmlRpcType = XmlRpcTypeInfo.GetXmlRpcType(valType); if (xmlRpcType == XmlRpcType.tMultiDimArray) { mappingStack.Push("array mapped to type " + valType.Name); Object ret = MapMultiDimArray(iter, valType, mappingStack, mappingAction); return(ret); } mappingStack.Push("array mapped to type " + valType.Name); } else { mappingStack.Push("array"); } var values = new List <object>(); Type elemType = DetermineArrayItemType(valType); bool bGotType = false; Type useType = null; while (iter.MoveNext() && iter.Current is ValueNode) { mappingStack.Push(String.Format("element {0}", values.Count)); object value = MapValueNode(iter, elemType, mappingStack, mappingAction); values.Add(value); mappingStack.Pop(); } foreach (object value in values) { if (value == null) { continue; } if (bGotType == false) { useType = value.GetType(); bGotType = true; } else { if (useType != value.GetType()) { useType = null; } } } Object[] args = new Object[1]; args[0] = values.Count; Object retObj = null; if (valType != null && valType != typeof(Array) && valType != typeof(object)) { retObj = CreateArrayInstance(valType, args); } else { if (useType == null) { retObj = CreateArrayInstance(typeof(object[]), args); } else { retObj = Array.CreateInstance(useType, (int)args[0]); }; } for (int j = 0; j < values.Count; j++) { ((Array)retObj).SetValue(values[j], j); } mappingStack.Pop(); return(retObj); }
private object MapStruct(IEnumerator <Node> iter, Type valueType, MappingStack mappingStack, MappingAction mappingAction, out Type mappedType) { mappedType = null; if (valueType.GetTypeInfo().IsPrimitive) { throw new XmlRpcTypeMismatchException(mappingStack.MappingType + " contains struct value where " + XmlRpcTypeInfo.GetXmlRpcTypeString(valueType) + " expected " + StackDump(mappingStack)); } if (valueType.GetTypeInfo().IsGenericType && valueType.GetGenericTypeDefinition() == typeof(Nullable <>)) { valueType = valueType.GetGenericArguments()[0]; } object retObj; try { retObj = Activator.CreateInstance(valueType); } catch (Exception) { throw new XmlRpcTypeMismatchException(mappingStack.MappingType + " contains struct value where " + XmlRpcTypeInfo.GetXmlRpcTypeString(valueType) + " expected (as type " + valueType.Name + ") " + StackDump(mappingStack)); } // Note: mapping action on a struct is only applied locally - it // does not override the global mapping action when members of the // struct are mapped MappingAction localAction = mappingAction; if (valueType != null) { mappingStack.Push("struct mapped to type " + valueType.Name); localAction = StructMappingAction(valueType, mappingAction); } else { mappingStack.Push("struct"); } // create map of field names and remove each name from it as // processed so we can determine which fields are missing var names = new List <string>(); CreateFieldNamesMap(valueType, names); int fieldCount = 0; List <string> rpcNames = new List <string>(); try { while (iter.MoveNext()) { if (!(iter.Current is StructMember)) { break; } string rpcName = (iter.Current as StructMember).Value; if (rpcNames.Contains(rpcName)) { if (!IgnoreDuplicateMembers) { throw new XmlRpcInvalidXmlRpcException(mappingStack.MappingType + " contains struct value with duplicate member " + rpcName + " " + StackDump(mappingStack)); } else { continue; } } else { rpcNames.Add(rpcName); } string name = GetStructName(valueType, rpcName) ?? rpcName; MemberInfo mi = valueType.GetField(name); if (mi == null) { mi = valueType.GetProperty(name); } if (mi == null) { iter.MoveNext(); // move to value if (iter.Current is ComplexValueNode) { int depth = iter.Current.Depth; while (!(iter.Current is EndComplexValueNode && iter.Current.Depth == depth)) { iter.MoveNext(); } } continue; } if (names.Contains(name)) { names.Remove(name); } else { //if (Attribute.IsDefined(mi, typeof(NonSerializedAttribute))) //{ // mappingStack.Push(String.Format("member {0}", name)); // throw new XmlRpcNonSerializedMember("Cannot map XML-RPC struct " // + "member onto member marked as [NonSerialized]: " // + " " + StackDump(mappingStack)); //} } Type memberType = mi is FieldInfo ? (mi as FieldInfo).FieldType : (mi as PropertyInfo).PropertyType; string mappingMsg = valueType == null ? String.Format("member {0}", name) : String.Format("member {0} mapped to type {1}", name, memberType.Name); iter.MoveNext(); object valObj = OnStack(mappingMsg, mappingStack, delegate() { return(MapValueNode(iter, memberType, mappingStack, mappingAction)); }); if (mi is FieldInfo) { (mi as FieldInfo).SetValue(retObj, valObj); } else { (mi as PropertyInfo).SetValue(retObj, valObj, null); } fieldCount++; } if (localAction == MappingAction.Error && names.Count > 0) { ReportMissingMembers(valueType, names, mappingStack); } return(retObj); } finally { mappingStack.Pop(); } }
//#endif void Serialize( XmlWriter xtw, Object o, MappingActions mappingActions, List <object> nestedObjs) { if (nestedObjs.Contains(o)) { throw new XmlRpcUnsupportedTypeException(nestedObjs[0].GetType(), "Cannot serialize recursive data structure"); } nestedObjs.Add(o); try { xtw.WriteStartElement("", "value", ""); XmlRpcType xType = XmlRpcTypeInfo.GetXmlRpcType(o); if (xType == XmlRpcType.tArray) { xtw.WriteStartElement("", "array", ""); xtw.WriteStartElement("", "data", ""); Array a = (Array)o; foreach (Object aobj in a) { //if (aobj == null) // throw new XmlRpcMappingSerializeException(String.Format( // "Items in array cannot be null ({0}[]).", //o.GetType().GetElementType())); Serialize(xtw, aobj, mappingActions, nestedObjs); } WriteFullEndElement(xtw); WriteFullEndElement(xtw); } else if (xType == XmlRpcType.tMultiDimArray) { Array mda = (Array)o; int[] indices = new int[mda.Rank]; BuildArrayXml(xtw, mda, 0, indices, mappingActions, nestedObjs); } else if (xType == XmlRpcType.tBase64) { byte[] buf = (byte[])o; xtw.WriteStartElement("", "base64", ""); xtw.WriteBase64(buf, 0, buf.Length); WriteFullEndElement(xtw); } else if (xType == XmlRpcType.tBoolean) { bool boolVal = (bool)o; if (boolVal) { WriteFullElementString(xtw, "boolean", "1"); } else { WriteFullElementString(xtw, "boolean", "0"); } } else if (xType == XmlRpcType.tDateTime) { DateTime dt = (DateTime)o; string sdt = dt.ToString("yyyyMMdd'T'HH':'mm':'ss", DateTimeFormatInfo.InvariantInfo); WriteFullElementString(xtw, "dateTime.iso8601", sdt); } else if (xType == XmlRpcType.tDouble) { double doubleVal = (double)o; WriteFullElementString(xtw, "double", doubleVal.ToString(null, CultureInfo.InvariantCulture)); } else if (xType == XmlRpcType.tHashtable) { xtw.WriteStartElement("", "struct", ""); XmlRpcStruct xrs = o as XmlRpcStruct; foreach (object obj in xrs.Keys) { string skey = obj as string; xtw.WriteStartElement("", "member", ""); WriteFullElementString(xtw, "name", skey); Serialize(xtw, xrs[skey], mappingActions, nestedObjs); WriteFullEndElement(xtw); } WriteFullEndElement(xtw); } else if (xType == XmlRpcType.tInt32) { o = SerializeInt32(xtw, o, mappingActions); } else if (xType == XmlRpcType.tInt64) { o = SerializeInt64(xtw, o, mappingActions); } else if (xType == XmlRpcType.tString) { SerializeString(xtw, o); } else if (xType == XmlRpcType.tStruct) { MappingActions structActions = GetMappingActions(o.GetType().GetTypeInfo(), mappingActions); xtw.WriteStartElement("", "struct", ""); MemberInfo[] mis = o.GetType().GetMembers(); foreach (MemberInfo mi in mis) { if (mi.GetCustomAttribute <IgnoreDataMemberAttribute>() != null) { continue; } if (mi is FieldInfo) { FieldInfo fi = (FieldInfo)mi; string member = fi.Name; Attribute attrchk = fi.GetCustomAttribute <XmlRpcMemberAttribute>(); if (attrchk != null && attrchk is XmlRpcMemberAttribute) { string mmbr = ((XmlRpcMemberAttribute)attrchk).Member; if (mmbr != "") { member = mmbr; } } MappingActions memberActions = MemberMappingActions(o.GetType(), fi.Name, structActions); if (fi.GetValue(o) == null) { if (memberActions.NullMappingAction == NullMappingAction.Ignore) { continue; } else if (memberActions.NullMappingAction == NullMappingAction.Error) { throw new XmlRpcMappingSerializeException(@"Member """ + member + @""" of struct """ + o.GetType().Name + @""" cannot be null."); } } xtw.WriteStartElement("", "member", ""); WriteFullElementString(xtw, "name", member); Serialize(xtw, fi.GetValue(o), memberActions, nestedObjs); WriteFullEndElement(xtw); } else if (mi is PropertyInfo) { PropertyInfo pi = (PropertyInfo)mi; string member = pi.Name; Attribute attrchk = pi.GetCustomAttribute <XmlRpcMemberAttribute>(); if (attrchk != null && attrchk is XmlRpcMemberAttribute) { string mmbr = ((XmlRpcMemberAttribute)attrchk).Member; if (mmbr != "") { member = mmbr; } } MappingActions memberActions = MemberMappingActions(o.GetType(), pi.Name, structActions); if (pi.GetValue(o, null) == null) { if (memberActions.NullMappingAction == NullMappingAction.Ignore) { continue; } else if (memberActions.NullMappingAction == NullMappingAction.Error) { throw new XmlRpcMappingSerializeException(@"Member """ + member + @""" of struct """ + o.GetType().Name + @""" cannot be null."); } } xtw.WriteStartElement("", "member", ""); WriteFullElementString(xtw, "name", member); Serialize(xtw, pi.GetValue(o, null), memberActions, nestedObjs); WriteFullEndElement(xtw); } } WriteFullEndElement(xtw); } else if (xType == XmlRpcType.tVoid) { WriteFullElementString(xtw, "string", ""); } else if (xType == XmlRpcType.tNil) { xtw.WriteStartElement("nil"); WriteFullEndElement(xtw); } else { throw new XmlRpcUnsupportedTypeException(o.GetType()); } WriteFullEndElement(xtw); } catch (global::System.NullReferenceException) { throw new XmlRpcNullReferenceException("Attempt to serialize data " + "containing null reference"); } finally { nestedObjs.RemoveAt(nestedObjs.Count - 1); } }