Skip to content

adding multiple server stanzas to an http block; bug? #13

Open
@sidler-mozilla

Description

@sidler-mozilla
In [1]: from nginx.config.api import Config, Section, Location, KeyOption, KeyValueOption, KeyMultiValueOption                                                                                        

In [2]: http = Section('http', include='../conf/mime.types')                                       

In [3]: server1 = Section('server', KeyValueOption('listen', '80'), KeyValueOption('server_name', 'server1.com'), KeyMultiValueOption('return', [301, 'https://$server_name$request_uri']))           

In [4]: server2 = Section('server', KeyValueOption('listen', '443'), KeyValueOption('server_name', 'server2.com'), KeyMultiValueOption('return', [302, 'https://$server_name$request_uri']))          

In [5]: http.sections.add(server1, server2)      

In [6]: http                                     
Out[6]: 

http {
    include ../conf/mime.types;
    server {
        listen 443;
        server_name server2.com;
        return 302 https://$server_name$request_uri;
    }
}

I then make a very small change to the code and run it again:

 sidler@76  ~  cd repos/scottidler/nginx-config-builder/src 
 sidler@76  ~/repos/scottidler/nginx-config-builder/src   master ●  git diff
diff --git i/src/nginx/config/api/options.py w/src/nginx/config/api/options.py
index 18f8945..0b6db52 100644
--- i/src/nginx/config/api/options.py
+++ w/src/nginx/config/api/options.py
@@ -116,7 +116,6 @@ class AttrDict(dict):
         self._owner = owner
         return ret
 
-
 class AttrList(AttrDict):
     """ A dictionary/list hybrid that exposes values as attributes. """
     def __iter__(self):
@@ -125,7 +124,7 @@ class AttrList(AttrDict):
     def append(self, item):
         if hasattr(item, '_parent'):
             item._parent = self._owner
-        if hasattr(item, 'name'):
+        if hasattr(item, 'name') and item.name != 'server':
             self[item.name] = item
         else:
             self[hash(item)] = item
 sidler@76  ~/repos/scottidler/nginx-config-builder/src   master ●  sudo -E ipython3
Python 3.7.5 (default, Nov 20 2019, 09:21:52) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.10.2 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from nginx.config.api import Config, Section, Location, KeyOption, KeyValueOption, KeyMultiValueOption                                                                                        

In [2]: http = Section('http', include='../conf/mime.types')                                       

In [3]: server1 = Section('server', KeyValueOption('listen', '80'), KeyValueOption('server_name', 'server1.com'), KeyMultiValueOption('return', [301, 'https://$server_name$request_uri']))           

In [4]: server2 = Section('server', KeyValueOption('listen', '443'), KeyValueOption('server_name', 'server2.com'), KeyMultiValueOption('return', [302, 'https://$server_name$request_uri']))          

In [5]: http.sections.add(server1, server2)      

In [6]: http                                     
Out[6]: 

http {
    include ../conf/mime.types;
    server {
        listen 80;
        server_name server1.com;
        return 301 https://$server_name$request_uri;
    }
    server {
        listen 443;
        server_name server2.com;
        return 302 https://$server_name$request_uri;
    }
}

It seems what is happening here is that it tests to see if the object as a 'name' field and if so, it uses this for the hash of the dictionary. In the case of the server blocks, the 'name' field is 'server' and therefore it only allows for one server block. Is this intentional? Is this a bug? Or is there another way that this is supposed to be used?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions