Skip to content

Add a binary constant mode to StringValues #64

@lodejard

Description

@lodejard

Something like the following

        public StringValues(string value, Encoding encoding)
        {
            _value = value;
            _values = null;
            _bytes = encoding.GetBytes(value);
            _encoding = encoding;
        }

        public static StringValues CreateBinaryConstant(string value, Encoding encoding)
        {
            return new StringValues(value, encoding);
        }

        public bool TryGetBinaryConstant(out byte[] bytes, out Encoding encoding)
        {
            bytes = _bytes;
            encoding = _encoding;
            return _bytes != null;
        }

would enable an application to contain

  static readonly StringValues _textPlain = StringValues.CreateBinaryConstant("text/plain", Encoding.ASCII);

// and later
    httpResponse.Headers["Content-Type"] = _textPlain;

this would enable a server to optimize the header output

  byte[] bytes; Encoding encoding;
  if (theHeaderValues.TryGetBinaryConstant(out bytes, out encoding) && encoding == _ascii)
  {
    // blit "\r\nThe-Header: " 
    // blit {bytes}
  }
  else
  {
    foreach(var value in headerValues)
    {
      // blit "\r\nThe-Header: "
      // ascii encode {value}
    }
  }

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions