Skip to content

Scan should return output length#14

Open
ztbrown wants to merge 2 commits intofekberg:masterfrom
ztbrown:scan-return-output-length
Open

Scan should return output length#14
ztbrown wants to merge 2 commits intofekberg:masterfrom
ztbrown:scan-return-output-length

Conversation

@ztbrown
Copy link
Copy Markdown
Contributor

@ztbrown ztbrown commented Jul 25, 2017

this change will make scan return the length of the output string. Closes #13

this change will make scan return the length of the output string
@fekberg
Copy link
Copy Markdown
Owner

fekberg commented Jul 25, 2017

Does this change the overall behaviour?

@ztbrown
Copy link
Copy Markdown
Contributor Author

ztbrown commented Jul 25, 2017

I can see two places where I think it changes the behavior for the better: getHttpVersion and handleHttpGET.

On line 286 and 287 of getHttpVersion, we do this:

    int start = scan(input, filename, 4, 100);
    if ( start > 0 ) {
        ...
    }

Unless the start index is greater than strlen(input), the conditional will always be true, even if filename is empty. I would think that, if filename, which in this case is holding the HTTP version, is empty, getHttpVersion should immediately return a fail code.

On line 377 of handleHttpGET, we do this:

    fileNameLenght = scan(input, filename, 5, 200);
        if ( fileNameLenght > 0 )
        {
            ...
        }

This suffers from the same problem as handleHttpGET and is misleading to anyone who might be reading it for educational purposes. The variable says that it is holding the fileNameLength, when it is actually holding the length of the beginning of the input to the end of the output.

My solution isn't great, now that I'm looking at it again. In the spirit of being readable and exemplary, I should have returned strlen(output). I will make that update now.

This make it more obvious what we are doing and also saves us from an "off by one" bug that I nearly inflicted on us.
@fekberg
Copy link
Copy Markdown
Owner

fekberg commented Jul 25, 2017

Hmm. Then, the following would not be needed, right?

Look at the lines above it:

	i += 1;

	for (; i < strlen(input); i ++ )
	{
		if ( *(input + i ) != '\t' && *(input + i) != ' ' && *(input + i) != '\n' && *(input + i) != '\r')
			break;
	}

If it finds a tab, whitespace or a newline, it returns the amount of characters until that new segment.

Which is pretty much the same as doing strlen(output), right?

Just make sure it's working the same way!

@ztbrown
Copy link
Copy Markdown
Contributor Author

ztbrown commented Jul 25, 2017

@fekberg
Copy link
Copy Markdown
Owner

fekberg commented Jul 25, 2017

Excellent, have you tried using a browser as well?

Could you remove the dead code from this PR as well please?

@ztbrown
Copy link
Copy Markdown
Contributor Author

ztbrown commented Jul 25, 2017

Well, this is currently breaking stuff. I'll take a look later and find out why that's happening.

@ztbrown
Copy link
Copy Markdown
Contributor Author

ztbrown commented Jul 25, 2017

Ah, this is breaking on checkMime. Looking into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants