BOOL TNLRequestValidate()

in Source/TNLRequest.m [21:74]


BOOL TNLRequestValidate(id<TNLRequest> __nullable request,
                        TNLRequestConfiguration * __nullable config,
                        NSError * __nullable * __nullable errorOut)
{
    NSError *error = nil;
    NSURL *url = [request respondsToSelector:@selector(URL)] ? request.URL : nil;
    if (!url) {
        error = TNLErrorCreateWithCode(TNLErrorCodeRequestInvalid);
    } else if (!url.host || !url.scheme || url.isFileReferenceURL) {
        error = TNLErrorCreateWithCode(TNLErrorCodeRequestInvalidURL);
    } else {
        TNLHTTPMethod method = TNLRequestGetHTTPMethodValue(request);
        if (TNLHTTPMethodUnknown == method) {
            error = TNLErrorCreateWithCode(TNLErrorCodeRequestInvalidHTTPMethod);
        } else {
            const BOOL isDownload = (TNLResponseDataConsumptionModeSaveToDisk == config.responseDataConsumptionMode);
            const BOOL isBackground = (TNLRequestExecutionModeBackground == config.executionMode);
            union {
                struct {
                    BOOL data:1;
                    BOOL file:1;
                    BOOL stream:1;
                    char padding:5;
                } body;
                char hasBody;
            } bodyUnion;

            bodyUnion.hasBody = 0;
            bodyUnion.body.data = [request respondsToSelector:@selector(HTTPBody)] && nil != request.HTTPBody;
            bodyUnion.body.file = [request respondsToSelector:@selector(HTTPBodyFilePath)] && nil != request.HTTPBodyFilePath;
            bodyUnion.body.stream = [request respondsToSelector:@selector(HTTPBodyStream)] && nil != request.HTTPBodyStream;

            if (isBackground) {
                if (!isDownload) {
                    if (!bodyUnion.body.file && !bodyUnion.body.data) {
                        // upload must have a file or data for the body.
                        // nil and stream are invalid in the background
                        error = TNLErrorCreateWithCode(TNLErrorCodeRequestInvalidBackgroundRequest);
                    }
                }
            }

            if (isDownload && bodyUnion.hasBody) {
                error = TNLErrorCreateWithCode(TNLErrorCodeRequestHTTPBodyCannotBeSetForDownload);
            }
        }
    }

    if (errorOut) {
        *errorOut = error;
    }

    return !error;
}