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

Feature request to pass HTTP_ORIGIN with CORS #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

caseman72
Copy link

Chris,

I added the feature. The issue with the response it that you don't have the request object:
http://sysmagazine.com/posts/166539/

So I did the following:

  • wrapped the background.js in a method - to avoid global scope (this reduces warnings by ugilfyjs)
  • added a requestIds array / stack that caches the request data. I allow 10s before the request is GC'd
  • added a onBeforeSendHeaders handler - to find the request data
  • moved the onErrorOccurred to onErrorOccurredHandler to continue with the same logic and not stack the event handler on every change
  • added the logic to change HTTP_ORIGIN to "Origin/Referer/*" in that order
  • tested it with my garmin.com.js plugin - works great!!


//push on to stack
if (origin) {
requestIds.push({
Copy link
Owner

Choose a reason for hiding this comment

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

I'd recommend using an object hash instead of an array here. That way we won't need to iterate/filter the array later, we can just pull out the request by its ID:

requestIds = {};
requestIds[ 'ID_'+info.requestId ] = { origin: origin, expiration:1234 };

//when retrieving value
var request = requestIds[ 'ID_' + info.requestId ];

Copy link
Author

Choose a reason for hiding this comment

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

I started with a hash - but then you'd have to use delete() to remove items. Since the list isn't every going to be huge the looping speed will be small. Using an array - you can use the Array methods like '.find' and '.filter' ...

I don't like to remove items in a loop like this:

for (var id in hash) {
    delete(hash[id]);
}

Copy link
Owner

Choose a reason for hiding this comment

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

There's no need to loop through the IDs, you'll have the specific ID you need to delete:

delete requestIds[ 'ID_' + info.requestId ];
//or if you don't care about having an empty key
requestIds[ 'ID_' + info.requestId ] = null;

I don't see any situation in the code where you would iterate all of the keys...

Copy link
Author

Choose a reason for hiding this comment

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

How about when you have a request but no response? Your requestIds will grow ...

Copy link
Author

Choose a reason for hiding this comment

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

You would have to loop through the Object - looking for expired (non-response) - so since the list will be at most 100 (100 requests in 10s) - looping isn't a big deal ... using a hash seems like a good idea until you do for (var id in hash) - then looping is better in an array.

Copy link
Owner

Choose a reason for hiding this comment

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

valid point, the request should be removed from the requestIds inside the getRequestAndCleanUp method then.

Did you intend to periodically clean out 'expired' requests? I don't see that logic here

@caseman72
Copy link
Author

    requestIds = requestIds.filter(function(elem) {
        if (elem.requestId === requestId) {
            request = elem;
        }
        return (elem.expiration < now && elem.requestId !== requestId);
    });

This will find the element and filter expired ones and the current one ...

This may be better (less compares):

    requestIds = requestIds.filter(function(elem) {
        if (elem.requestId === requestId) {
            request = elem;
            return false; // remove
        }
        return (elem.expiration < now); // true -keep- if not expired
    });

@caseman72
Copy link
Author

Thanks! I found a bug with my filter... updating code now.

Logic was ok ... filter is weird as it true (keeps) and false (removes)

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