didi/DoKit · error · IllegalArgumentException

Unexpected header: " + line

Error message

Unexpected header: " + line

What it means

Thrown by CommonHeaders.Builder.add(String line) when the given header line contains no ':' character. The method splits a raw header line into name and value at the first colon; a colon-free line cannot be split so it is rejected.

Source

Thrown at Android/dokit/src/main/java/com/didichuxing/doraemonkit/kit/network/common/CommonHeaders.java:235

     */
    Builder addLenient(String line) {
      int index = line.indexOf(":", 1);
      if (index != -1) {
        return addLenient(line.substring(0, index), line.substring(index + 1));
      } else if (line.startsWith(":")) {
        // Work around empty header names and header names that start with a
        // colon (created by old broken SPDY versions of the response cache).
        return addLenient("", line.substring(1)); // Empty header name.
      } else {
        return addLenient("", line); // No header name.
      }
    }

    /** Add an header line containing a field name, a literal colon, and a value. */
    public Builder add(String line) {
      int index = line.indexOf(":");
      if (index == -1) {
        throw new IllegalArgumentException("Unexpected header: " + line);
      }
      return add(line.substring(0, index).trim(), line.substring(index + 1));
    }

    /** Add a field with the specified value. */
    public Builder add(String name, String value) {
      checkNameAndValue(name, value);
      return addLenient(name, value);
    }

    /**
     * Add a field with the specified value without any validation. Only appropriate for headers
     * from the remote peer or cache.
     */
    Builder addLenient(String name, String value) {
      namesAndValues.add(name);
      namesAndValues.add(value.trim());
      return this;

View on GitHub (pinned to 626827cddb)

Solutions

  1. Check indexOf(':') per line before calling add, and skip/log lines without one
  2. Fix the header-block parser to only yield header lines, not status lines or continuations
  3. For continuation lines (obs-fold), append to the previous header's value instead of adding a new line

Example fix

// before
for (String line : rawHeaderBlock.split("\n")) builder.add(line); // throws on "HTTP/1.1 200 OK"

// after
for (String line : rawHeaderBlock.split("\n")) {
  if (line.contains(":")) builder.add(line);
}
Defensive patterns

Strategy: validation

Validate before calling

for (String line : headerBlock.split("\r?\n")) {
  if (line != null && line.indexOf(':') != -1) builder.add(line);
}

Prevention

When it happens

Trigger: Calling builder.add("Content-Type application/json") (missing colon), or feeding it a line that is a bare value, a comment, or a trailing empty/malformed line from a raw header block split by newline without per-line validation.

Common situations: Parsing pasted or recorded HTTP header blocks (e.g. from DoKit mock templates or curl exports) where one line is a wrapped continuation or simply malformed; status lines accidentally included in the header list.

Related errors


AI-assisted analysis of didi/DoKit@626827cddb (2026-08-14). Data as JSON: /api/errors/5a3beed36f16fcf3. Report an issue: GitHub.