Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nsq_to_http: add custom header flag #1072

Merged
merged 1 commit into from
Sep 1, 2018
Merged

nsq_to_http: add custom header flag #1072

merged 1 commit into from
Sep 1, 2018

Conversation

alwindoss
Copy link
Contributor

Signed-off-by: Alwin Doss alwindoss84@gmail.com

@alwindoss alwindoss changed the title nsq_to_http: Added Customer header flag nsq_to_http: Added Custom header flag Aug 19, 2018
)

func init() {
flag.Var(&postAddrs, "post", "HTTP address to make a POST request to. data will be in the body (may be given multiple times)")
flag.Var(&customHeaders, "custom-header", "Custom header for HTTP requests (may be given multiple times)")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest making the flag shorter, just header (it would also match curl's --header flag)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, this would enable people who are comfortable with curl to use this option

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

func parseCustomHeaders(strs []string) (map[string]string, error) {
parsedHeaders := make(map[string]string)
for _, s := range strs {
sp := strings.Split(s, "=")
Copy link
Member

@ploxiln ploxiln Aug 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the curl --header flag previously mentioned, one passes the header more similarly to how it is sent in the request, with a : separating the key and value, e.g. --header "User-Agent: something something".

A = or : could appear in the value, e.g. WWW-Authenticate: Basic realm="notice: private system", so either way you should use SplitN() to limit to 2 substrings (only splitting on the first separator).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed this

@mreiferson mreiferson changed the title nsq_to_http: Added Custom header flag nsq_to_http: add custom header flag Aug 19, 2018
@mreiferson
Copy link
Member

Thanks for the contribution @alwindoss, this is coming out nice so far — looks like @ploxiln's got the code review 😁

@alwindoss
Copy link
Contributor Author

Added test and build/ to .gitignore

.gitignore Outdated
@@ -1,6 +1,8 @@
.godeps
vendor
./build
build/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the line right above this should already work to ignore the build folder

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(just the build folder at the very top of the repo directory tree)

.gitignore Outdated
@@ -1,6 +1,8 @@
.godeps
vendor
./build
build/
test
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will also ignore internal/test/ which has checked-in source code we don't want ignored

key := strings.TrimSpace(sp[0])
val := strings.TrimSpace(sp[1])
if key == "" || val == "" {
return nil, errors.New("Not meeting the criteria")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both of these error strings could be more specific, e.g. fmt.Errorf("Invalid header %q", s)

Then, above, your fatal logging could print just the (first) one which is invalid. Right now it does:

log.Printf("Specified custom headers: %v", validCustomHeaders)

which will be empty (maybe you meant customHeaders there)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the log.Printf("Specified custom headers: %v", validCustomHeaders) to a move appropriate place i.e., after the error check. Also, cleaned up the other logs

Copy link
Member

@mreiferson mreiferson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM after last minor comment

if err != nil {
log.Fatal("--header value format should be 'key=value'")
}
log.Printf("Specified custom headers: %v", validCustomHeaders)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we drop this log line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it does seem redundant. But technically I need help here. For some reason I keep getting an error that I have an unused variable called validCustomHeaders
I could use a lil help here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh, an annoying quirk of Go known as "variable shadowing". What you want on line 187 is this:

var err error
validCustomHeaders, err = parseCustomHeaders(customHeaders)
...

(note the lack of :=)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know Go does not allow you to have unused variables but "variable shadowing" I am hearing for the first time. I have fixed it.

var err error
validCustomHeaders, err = parseCustomHeaders(customHeaders)
if err != nil {
log.Fatal("--header value format should be 'key=value'")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could include the err string in the fatal message

@ploxiln
Copy link
Member

ploxiln commented Aug 28, 2018

Looks good to me. Can you squash these commits down to a single commit?

Signed-off-by: Alwin Doss <alwindoss84@gmail.com>

Resolved merge conflicts

Signed-off-by: Alwin Doss <alwindoss84@gmail.com>

Resolved merge conflicts

Signed-off-by: Alwin Doss <alwindoss84@gmail.com>

Edited the description of the test case

Signed-off-by: Alwin Doss <alwindoss84@gmail.com>

Make the header option look similar to header option in curl

Signed-off-by: Alwin Doss <alwindoss84@gmail.com>

Cleaned up the logging

Signed-off-by: Alwin Doss <alwindoss84@gmail.com>

Removed redundant log message

Signed-off-by: Alwin Doss <alwindoss84@gmail.com>
@alwindoss
Copy link
Contributor Author

Squashed the commits.

@ploxiln ploxiln merged commit 11778a1 into nsqio:master Sep 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants