1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.core5.http.message;
29
30 import org.apache.hc.core5.annotation.Contract;
31 import org.apache.hc.core5.annotation.ThreadingBehavior;
32 import org.apache.hc.core5.http.Header;
33 import org.apache.hc.core5.http.ProtocolVersion;
34 import org.apache.hc.core5.util.Args;
35 import org.apache.hc.core5.util.CharArrayBuffer;
36
37
38
39
40
41
42 @Contract(threading = ThreadingBehavior.IMMUTABLE)
43 public class BasicLineFormatter implements LineFormatter {
44
45 public final static BasicLineFormattericLineFormatter.html#BasicLineFormatter">BasicLineFormatter INSTANCE = new BasicLineFormatter();
46
47 public BasicLineFormatter() {
48 super();
49 }
50
51 void formatProtocolVersion(final CharArrayBuffer buffer, final ProtocolVersion version) {
52 buffer.append(version.format());
53 }
54
55 @Override
56 public void formatRequestLine(final CharArrayBuffer buffer, final RequestLine reqline) {
57 Args.notNull(buffer, "Char array buffer");
58 Args.notNull(reqline, "Request line");
59 buffer.append(reqline.getMethod());
60 buffer.append(' ');
61 buffer.append(reqline.getUri());
62 buffer.append(' ');
63 formatProtocolVersion(buffer, reqline.getProtocolVersion());
64 }
65
66 @Override
67 public void formatStatusLine(final CharArrayBuffer buffer, final StatusLine statusLine) {
68 Args.notNull(buffer, "Char array buffer");
69 Args.notNull(statusLine, "Status line");
70
71 formatProtocolVersion(buffer, statusLine.getProtocolVersion());
72 buffer.append(' ');
73 buffer.append(Integer.toString(statusLine.getStatusCode()));
74 buffer.append(' ');
75 final String reasonPhrase = statusLine.getReasonPhrase();
76 if (reasonPhrase != null) {
77 buffer.append(reasonPhrase);
78 }
79 }
80
81 @Override
82 public void formatHeader(final CharArrayBuffer buffer, final Header header) {
83 Args.notNull(buffer, "Char array buffer");
84 Args.notNull(header, "Header");
85
86 buffer.append(header.getName());
87 buffer.append(": ");
88 final String value = header.getValue();
89 if (value != null) {
90 buffer.ensureCapacity(buffer.length() + value.length());
91 for (int valueIndex = 0; valueIndex < value.length(); valueIndex++) {
92 char valueChar = value.charAt(valueIndex);
93 if (valueChar == '\r'
94 || valueChar == '\n'
95 || valueChar == '\f'
96 || valueChar == 0x0b) {
97 valueChar = ' ';
98 }
99 buffer.append(valueChar);
100 }
101 }
102 }
103
104 }