public BindingResult ConvertValues(IMultipartHttpEntity entity, Type targetType) { var sourceMediaType = entity.ContentType ?? MediaType.TextPlain; var type = this.typeSystem.FromClr(targetType); var mediaTypeReaderReg = this.codecs.FindMediaTypeReader(sourceMediaType, new[] { type }, null); if (mediaTypeReaderReg != null) { var mediaTypeReader = (ICodec)this.container.Resolve(mediaTypeReaderReg.CodecRegistration.CodecType); if (mediaTypeReader is IMediaTypeReader) { return BindingResult.Success(((IMediaTypeReader)mediaTypeReader).ReadFrom(entity, type, targetType.Name)); } var binder = this.BinderLocator.GetBinder(type); if (mediaTypeReader.TryAssignKeyValues(entity, binder)) { return binder.BuildObject(); } } // if no media type reader was found, try to parse to a string and convert from that. var stringType = this.typeSystem.FromClr<string>(); mediaTypeReaderReg = this.codecs.FindMediaTypeReader(sourceMediaType, new[] { stringType }, null); if (entity.ContentType == null) { entity.ContentType = MediaType.TextPlain; } // defaults the entity to UTF-8 if none is specified, to account for browsers favouring using the charset of the origin page rather than the standard. Cause RFCs are too difficult to follow uh... if (entity.ContentType.CharSet == null) { entity.ContentType.CharSet = "UTF-8"; } var plainTextReader = (IMediaTypeReader)this.container.Resolve(mediaTypeReaderReg.CodecRegistration.CodecType); var targetString = plainTextReader.ReadFrom(entity, stringType, targetType.Name); object destination = targetType.CreateInstanceFrom(targetString); return BindingResult.Success(destination); }
public BindingResult ConvertValues(IMultipartHttpEntity entity, Type targetType) { object destination; var sourceMediaType = entity.ContentType ?? MediaType.TextPlain; var type = _typeSystem.FromClr(targetType); var mediaTypeReaderReg = _codecs.FindMediaTypeReader(sourceMediaType, new[] { type }, null); if (mediaTypeReaderReg != null) { var mediaTypeReader = (ICodec)_container.Resolve(mediaTypeReaderReg.CodecRegistration.CodecType); if (mediaTypeReader is IMediaTypeReader) { return(BindingResult.Success(((IMediaTypeReader)mediaTypeReader).ReadFrom(entity, type, targetType.Name))); } var binder = BinderLocator.GetBinder(type); if (mediaTypeReader.TryAssignKeyValues(entity, binder)) { return(binder.BuildObject()); } } // if no media type reader was found, try to parse to a string and convert from that. var stringType = _typeSystem.FromClr <string>(); mediaTypeReaderReg = _codecs.FindMediaTypeReader(sourceMediaType, new[] { stringType }, null); if (entity.ContentType == null) { entity.ContentType = MediaType.TextPlain; } // defaults the entity to UTF-8 if none is specified, to account for browsers favouring using the charset of the origin page rather than the standard. Cause RFCs are too difficult to follow uh... if (entity.ContentType.CharSet == null) { entity.ContentType.CharSet = "UTF-8"; } var plainTextReader = (IMediaTypeReader)_container.Resolve(mediaTypeReaderReg.CodecRegistration.CodecType); var targetString = plainTextReader.ReadFrom(entity, stringType, targetType.Name); destination = targetType.CreateInstanceFrom(targetString); return(BindingResult.Success(destination)); }
private bool ReadEntity() { if (this.AtEndBoundary) { return false; } var entity = new MultipartHttpEntity(); // TODO: Handle split headers while (this.ReadNextLine() && !string.IsNullOrEmpty(this.currentLine) && !this.AtBoundary && !this.AtEndBoundary) { int columnIndex = this.currentLine.IndexOf(":"); if (columnIndex != -1) { entity.Headers[this.currentLine.Substring(0, columnIndex).Trim()] = this.currentLine.Substring(columnIndex + 1).Trim(); } } if (this.currentLine == null) { return false; } if (this.currentLine.Length == 0) { entity.Stream = this.reader.GetNextPart(); } this.CurrentEntity = entity; return true; }
bool ReadEntity() { if (AtEndBoundary) return false; var entity = new MultipartHttpEntity(); // TODO: Handle split headers while (ReadNextLine() && !string.IsNullOrEmpty(_currentLine) && !AtBoundary && !AtEndBoundary) { var columnIndex = _currentLine.IndexOf(":", StringComparison.Ordinal); if (columnIndex != -1) entity.Headers[_currentLine.Substring(0, columnIndex).Trim()] = _currentLine.Substring(columnIndex + 1).Trim(); } if (_currentLine == null) { entity.Dispose(); return false; } if (_currentLine.Length == 0) entity.Stream = _reader.GetNextPart(); CurrentEntity = entity; return true; }