ObjFW  Check-in [12c09ef41e]

Overview
Comment:Add OFHTTPRequestMethodString()

This deprecates OFHTTPRequestMethodName(), which returns a C string.
APIs should avoid C strings as much as possible.

This function was initially only used internally, where this was fine.
However, when it was made public, it should have been converted to
OFString at the same time.

Adds OFHTTPRequestMethodParseString() for consistency, which behaves the
same as OFHTTPRequestMethodParseName() and deprecates it.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 12c09ef41ec35c30d966670daaa63fdb34d283c7615d8176cba30ecdf7917a2a
User & Date: js on 2023-10-15 14:55:50
Other Links: manifest | tags
Context
2023-10-15
14:59
macros.h: Add missing #else check-in: f2891af000 user: js tags: trunk
14:57
Merge trunk into branch "ofhttpd" Leaf check-in: e2a818c0b5 user: js tags: ofhttpd
14:55
Add OFHTTPRequestMethodString() check-in: 12c09ef41e user: js tags: trunk
14:53
macros.h: Add OF_DEPRECATED check-in: b84438dcfd user: js tags: trunk
Changes

Modified src/OFHTTPClient.m from [0348d66062] to [771a949bd6].

128
129
130
131
132
133
134
135

136
137
138
139
140
141
142
128
129
130
131
132
133
134

135
136
137
138
139
140
141
142







-
+








	if (IRI.path.length > 0)
		path = IRI.percentEncodedPath;
	else
		path = @"/";

	requestString = [OFMutableString stringWithFormat:
	    @"%s %@", OFHTTPRequestMethodName(method), path];
	    @"%@ %@", OFHTTPRequestMethodString(method), path];

	if (IRI.query != nil) {
		[requestString appendString: @"?"];
		[requestString appendString: IRI.percentEncodedQuery];
	}

	[requestString appendString: @" HTTP/"];

Modified src/OFHTTPRequest.h from [163a3d1bed] to [ff8f88f432].

136
137
138
139
140
141
142
143

144
145
146


147
148

149
150
151
152
153
154
155
156
157
158























159


160
161
162
163
164
136
137
138
139
140
141
142

143
144


145
146
147

148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181

182
183
184
185
186
187
188







-
+

-
-
+
+

-
+










+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+





- (instancetype)init OF_UNAVAILABLE;
@end

#ifdef __cplusplus
extern "C" {
#endif
/**
 * @brief Returns a C string describing the specified request method.
 * @brief Returns a string describing the specified request method.
 *
 * @param method The request method which should be described as a C string
 * @return A C string describing the specified request method
 * @param method The request method which should be described as a string
 * @return A string describing the specified request method
 */
extern const char *_Nullable OFHTTPRequestMethodName(
extern OFString *_Nullable OFHTTPRequestMethodString(
    OFHTTPRequestMethod method);

/**
 * @brief Returns the request method for the specified string.
 *
 * @param string The string for which the request method should be returned
 * @return The request method for the specified string
 * @throw OFInvalidFormatException The specified string is not a valid HTTP
 *				   request method
 */
extern OFHTTPRequestMethod OFHTTPRequestMethodParseString(OFString *string);

/**
 * @brief Returns a C string describing the specified request method.
 *
 * @deprecated Use @ref OFHTTPRequestMethodString instead.
 *
 * @param method The request method which should be described as a C string
 * @return A C string describing the specified request method
 */
extern const char *_Nullable OFHTTPRequestMethodName(OFHTTPRequestMethod method)
    OF_DEPRECATED(ObjFW, 1, 1, "Use OFHTTPRequestMethodString instead");

/**
 * @brief Returns the request method for the specified string.
 *
 * @deprecated Use @ref OFHTTPRequestMethodParseString instead.
 *
 * @param string The string for which the request method should be returned
 * @return The request method for the specified string
 * @throw OFInvalidFormatException The specified string is not a valid HTTP
 *				   request method
 */
extern OFHTTPRequestMethod OFHTTPRequestMethodParseName(OFString *string);
extern OFHTTPRequestMethod OFHTTPRequestMethodParseName(OFString *string)
    OF_DEPRECATED(ObjFW, 1, 1, "Use OFHTTPRequestMethodParseString instead");
#ifdef __cplusplus
}
#endif

OF_ASSUME_NONNULL_END

Modified src/OFHTTPRequest.m from [f630f4e80b] to [7d8bd2c275].

25
26
27
28
29
30
31
32
33


34
35
36
37

38
39

40
41

42
43

44
45

46
47

48
49

50
51

52
53
54

55
56
57
58

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78














79
80
81
82
83
84
85
25
26
27
28
29
30
31


32
33
34
35
36

37
38

39
40

41
42

43
44

45
46

47
48

49
50

51
52
53

54
55
56
57

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99







-
-
+
+



-
+

-
+

-
+

-
+

-
+

-
+

-
+

-
+


-
+



-
+




















+
+
+
+
+
+
+
+
+
+
+
+
+
+







#import "OFString.h"

#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFOutOfRangeException.h"
#import "OFUnsupportedVersionException.h"

const char *
OFHTTPRequestMethodName(OFHTTPRequestMethod method)
OFString *
OFHTTPRequestMethodString(OFHTTPRequestMethod method)
{
	switch (method) {
	case OFHTTPRequestMethodOptions:
		return "OPTIONS";
		return @"OPTIONS";
	case OFHTTPRequestMethodGet:
		return "GET";
		return @"GET";
	case OFHTTPRequestMethodHead:
		return "HEAD";
		return @"HEAD";
	case OFHTTPRequestMethodPost:
		return "POST";
		return @"POST";
	case OFHTTPRequestMethodPut:
		return "PUT";
		return @"PUT";
	case OFHTTPRequestMethodDelete:
		return "DELETE";
		return @"DELETE";
	case OFHTTPRequestMethodTrace:
		return "TRACE";
		return @"TRACE";
	case OFHTTPRequestMethodConnect:
		return "CONNECT";
		return @"CONNECT";
	}

	return NULL;
	return nil;
}

OFHTTPRequestMethod
OFHTTPRequestMethodParseName(OFString *string)
OFHTTPRequestMethodParseString(OFString *string)
{
	if ([string isEqual: @"OPTIONS"])
		return OFHTTPRequestMethodOptions;
	if ([string isEqual: @"GET"])
		return OFHTTPRequestMethodGet;
	if ([string isEqual: @"HEAD"])
		return OFHTTPRequestMethodHead;
	if ([string isEqual: @"POST"])
		return OFHTTPRequestMethodPost;
	if ([string isEqual: @"PUT"])
		return OFHTTPRequestMethodPut;
	if ([string isEqual: @"DELETE"])
		return OFHTTPRequestMethodDelete;
	if ([string isEqual: @"TRACE"])
		return OFHTTPRequestMethodTrace;
	if ([string isEqual: @"CONNECT"])
		return OFHTTPRequestMethodConnect;

	@throw [OFInvalidFormatException exception];
}

/* Deprecated */
const char *
OFHTTPRequestMethodName(OFHTTPRequestMethod method)
{
	return OFHTTPRequestMethodString(method).UTF8String;
}

/* Deprecated */
OFHTTPRequestMethod
OFHTTPRequestMethodParseName(OFString *string)
{
	return OFHTTPRequestMethodParseString(string);
}

@implementation OFHTTPRequest
@synthesize IRI = _IRI, method = _method, headers = _headers;

+ (instancetype)requestWithIRI: (OFIRI *)IRI
{
	return [[[self alloc] initWithIRI: IRI] autorelease];
239
240
241
242
243
244
245
246

247
248
249
250
251
252
253
254
255
256
257
258
259
260

261
262
263
264
265
266
267
268
269
270
253
254
255
256
257
258
259

260
261
262
263
264
265
266
267
268
269
270
271
272
273

274
275
276
277
278
279
280
281
282
283
284







-
+













-
+










					   _protocolVersion.major,
					   _protocolVersion.minor];
}

- (OFString *)description
{
	void *pool = objc_autoreleasePoolPush();
	const char *method = OFHTTPRequestMethodName(_method);
	OFString *method = OFHTTPRequestMethodString(_method);
	OFString *indentedHeaders, *remoteAddress, *ret;

	indentedHeaders = [_headers.description
	    stringByReplacingOccurrencesOfString: @"\n"
				      withString: @"\n\t"];

	if (_hasRemoteAddress)
		remoteAddress = OFSocketAddressString(&_remoteAddress);
	else
		remoteAddress = nil;

	ret = [[OFString alloc] initWithFormat:
	    @"<%@:\n\tIRI = %@\n"
	    @"\tMethod = %s\n"
	    @"\tMethod = %@\n"
	    @"\tHeaders = %@\n"
	    @"\tRemote address = %@\n"
	    @">",
	    self.class, _IRI, method, indentedHeaders, remoteAddress];

	objc_autoreleasePoolPop(pool);

	return [ret autorelease];
}
@end

Modified src/OFHTTPServer.m from [ab884b4ab8] to [c229043b21].

378
379
380
381
382
383
384
385

386
387
388
389
390
391
392
378
379
380
381
382
383
384

385
386
387
388
389
390
391
392







-
+








	pos = [line rangeOfString: @" "].location;
	if (pos == OFNotFound)
		return [self sendErrorAndClose: 400];

	method = [line substringToIndex: pos];
	@try {
		_method = OFHTTPRequestMethodParseName(method);
		_method = OFHTTPRequestMethodParseString(method);
	} @catch (OFInvalidArgumentException *e) {
		return [self sendErrorAndClose: 405];
	}

	@try {
		OFRange range = OFMakeRange(pos + 1, line.length - pos - 10);

Modified src/exceptions/OFHTTPRequestFailedException.m from [fd787d72c8] to [4a244e4e24].

57
58
59
60
61
62
63
64

65
66
67

68
69
70
57
58
59
60
61
62
63

64
65
66

67
68
69
70







-
+


-
+



	[_response release];

	[super dealloc];
}

- (OFString *)description
{
	const char *method = OFHTTPRequestMethodName(_request.method);
	OFString *method = OFHTTPRequestMethodString(_request.method);

	return [OFString stringWithFormat:
	    @"An HTTP %s request with IRI %@ failed with code %hd!", method,
	    @"An HTTP %@ request with IRI %@ failed with code %hd!", method,
	    _request.IRI, _response.statusCode];
}
@end

Modified utils/ofhttp/OFHTTP.m from [d2650dbe5b] to [336f94ad28].

369
370
371
372
373
374
375
376

377
378
379
380
381
382
383
369
370
371
372
373
374
375

376
377
378
379
380
381
382
383







-
+







- (void)setMethod: (OFString *)method
{
	void *pool = objc_autoreleasePoolPush();

	method = method.uppercaseString;

	@try {
		_method = OFHTTPRequestMethodParseName(method);
		_method = OFHTTPRequestMethodParseString(method);
	} @catch (OFInvalidArgumentException *e) {
		[OFStdErr writeLine: OF_LOCALIZED(@"invalid_input_method",
		    @"%[prog]: Invalid request method %[method]!",
		    @"prog", [OFApplication programName],
		    @"method", method)];
		[OFApplication terminateWithStatus: 1];
	}