You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

260 lines
41 KiB

2 years ago
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.7"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Webserv: srcs/sock/Master.cpp Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<td id="projectalign">
<div id="projectname">Webserv
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.7 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search/",'.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
initMenu('',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>
<div id="nav-path" class="navpath">
<ul>
<li class="navelem"><a class="el" href="dir_b73b02d482d1c4a3db20c3d9ad52d395.html">srcs</a></li><li class="navelem"><a class="el" href="dir_b0f7ce2339e35a0e5afcbb3929376e00.html">sock</a></li> </ul>
</div>
</div><!-- top -->
<div class="header">
<div class="headertitle"><div class="title">Master.cpp</div></div>
</div><!--header-->
<div class="contents">
<a href="Master_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a id="l00001" name="l00001"></a><span class="lineno"> 1</span> </div>
<div class="line"><a id="l00008" name="l00008"></a><span class="lineno"> 8</span><span class="preprocessor">#include &quot;<a class="code" href="webserv_8hpp.html">webserv.hpp</a>&quot;</span></div>
<div class="line"><a id="l00009" name="l00009"></a><span class="lineno"> 9</span><span class="comment">/* Master destructor */</span></div>
<div class="line"><a id="l00010" name="l00010"></a><span class="lineno"> 10</span> </div>
<div class="line"><a id="l00011" name="l00011"></a><span class="lineno"><a class="line" href="classMaster.html#aa76c335c36c93e8134115733938651df"> 11</a></span><a class="code hl_function" href="classMaster.html#aa76c335c36c93e8134115733938651df">Master::~Master</a>(<span class="keywordtype">void</span>) {</div>
<div class="line"><a id="l00012" name="l00012"></a><span class="lineno"> 12</span> close(<a class="code hl_variable" href="classMaster.html#aee2fccb9a95dd83c89049c86a86ff5f3">_fd</a>);</div>
<div class="line"><a id="l00013" name="l00013"></a><span class="lineno"> 13</span> cout &lt;&lt; <span class="stringliteral">&quot;Destroyed master socket\n&quot;</span>;</div>
<div class="line"><a id="l00014" name="l00014"></a><span class="lineno"> 14</span>}</div>
<div class="line"><a id="l00015" name="l00015"></a><span class="lineno"> 15</span><span class="comment">/* |==========|</span></div>
<div class="line"><a id="l00016" name="l00016"></a><span class="lineno"> 16</span><span class="comment"> * Master constructor</span></div>
<div class="line"><a id="l00017" name="l00017"></a><span class="lineno"> 17</span><span class="comment"> * Try to create a socket listening to ip and port defined by input.</span></div>
<div class="line"><a id="l00018" name="l00018"></a><span class="lineno"> 18</span><span class="comment"> * If the creation success, the socket is then ready to select for new clients.</span></div>
<div class="line"><a id="l00019" name="l00019"></a><span class="lineno"> 19</span><span class="comment"> *</span></div>
<div class="line"><a id="l00020" name="l00020"></a><span class="lineno"> 20</span><span class="comment"> * Input: A ip_port_t structure which contain the ip and the port the master</span></div>
<div class="line"><a id="l00021" name="l00021"></a><span class="lineno"> 21</span><span class="comment"> * care about.</span></div>
<div class="line"><a id="l00022" name="l00022"></a><span class="lineno"> 22</span><span class="comment"> * Output: A Master object.</span></div>
<div class="line"><a id="l00023" name="l00023"></a><span class="lineno"> 23</span><span class="comment"> */</span></div>
<div class="line"><a id="l00024" name="l00024"></a><span class="lineno"> 24</span> </div>
<div class="line"><a id="l00025" name="l00025"></a><span class="lineno"><a class="line" href="classMaster.html#aa32552c9c965ac5d53d2cb5e65b41b36"> 25</a></span><a class="code hl_function" href="classMaster.html#aa32552c9c965ac5d53d2cb5e65b41b36">Master::Master</a>(<a class="code hl_struct" href="structip__port__s.html">ip_port_t</a> list) : _listen(list) {</div>
<div class="line"><a id="l00026" name="l00026"></a><span class="lineno"> 26</span> <span class="keywordtype">int</span> x = 1, port = <a class="code hl_variable" href="classMaster.html#af5432a935125075c1eb03aa9e8c8f894">_listen</a>.<a class="code hl_variable" href="structip__port__s.html#aae56f9ac0aa0193aaae11be127aa578e">port</a>;</div>
<div class="line"><a id="l00027" name="l00027"></a><span class="lineno"> 27</span> <span class="keywordtype">string</span> ip = <a class="code hl_variable" href="classMaster.html#af5432a935125075c1eb03aa9e8c8f894">_listen</a>.<a class="code hl_variable" href="structip__port__s.html#afb20776a9723a50ac32fd9b8248b3e0c">ip</a>;</div>
<div class="line"><a id="l00028" name="l00028"></a><span class="lineno"> 28</span> </div>
<div class="line"><a id="l00029" name="l00029"></a><span class="lineno"> 29</span> <a class="code hl_variable" href="classMaster.html#aee2fccb9a95dd83c89049c86a86ff5f3">_fd</a> = socket(AF_INET, SOCK_STREAM, 0);</div>
<div class="line"><a id="l00030" name="l00030"></a><span class="lineno"> 30</span> <span class="keywordflow">if</span> (<a class="code hl_variable" href="classMaster.html#aee2fccb9a95dd83c89049c86a86ff5f3">_fd</a> == 0)</div>
<div class="line"><a id="l00031" name="l00031"></a><span class="lineno"> 31</span> <span class="keywordflow">throw</span> std::runtime_error(<span class="stringliteral">&quot;socket() error&quot;</span> + <span class="keywordtype">string</span>(strerror(errno)));</div>
<div class="line"><a id="l00032" name="l00032"></a><span class="lineno"> 32</span> <span class="keywordflow">if</span> (setsockopt(<a class="code hl_variable" href="classMaster.html#aee2fccb9a95dd83c89049c86a86ff5f3">_fd</a>, SOL_SOCKET, SO_REUSEADDR, (<span class="keywordtype">char</span> *)&amp;x, <span class="keyword">sizeof</span>(x)) &lt; 0 &amp;&amp; close(<a class="code hl_variable" href="classMaster.html#aee2fccb9a95dd83c89049c86a86ff5f3">_fd</a>) &lt;= 0)</div>
<div class="line"><a id="l00033" name="l00033"></a><span class="lineno"> 33</span> <span class="keywordflow">throw</span> std::runtime_error(<span class="stringliteral">&quot;setsockopt() error: &quot;</span> + <span class="keywordtype">string</span>(strerror(errno)));</div>
<div class="line"><a id="l00034" name="l00034"></a><span class="lineno"> 34</span> </div>
<div class="line"><a id="l00035" name="l00035"></a><span class="lineno"> 35</span> <a class="code hl_variable" href="classMaster.html#a2930920309de02e86050a8facdc83b68">_address</a>.sin_family = AF_INET;</div>
<div class="line"><a id="l00036" name="l00036"></a><span class="lineno"> 36</span> <a class="code hl_variable" href="classMaster.html#a2930920309de02e86050a8facdc83b68">_address</a>.sin_addr.s_addr = inet_addr(ip.c_str());</div>
<div class="line"><a id="l00037" name="l00037"></a><span class="lineno"> 37</span> <a class="code hl_variable" href="classMaster.html#a2930920309de02e86050a8facdc83b68">_address</a>.sin_port = htons(port);</div>
<div class="line"><a id="l00038" name="l00038"></a><span class="lineno"> 38</span> </div>
<div class="line"><a id="l00039" name="l00039"></a><span class="lineno"> 39</span> <span class="keywordflow">if</span> (bind(<a class="code hl_variable" href="classMaster.html#aee2fccb9a95dd83c89049c86a86ff5f3">_fd</a>, (<span class="keyword">struct</span> sockaddr *)&amp;<a class="code hl_variable" href="classMaster.html#a2930920309de02e86050a8facdc83b68">_address</a>, <span class="keyword">sizeof</span>(<a class="code hl_variable" href="classMaster.html#a2930920309de02e86050a8facdc83b68">_address</a>)) &amp;&amp; close(<a class="code hl_variable" href="classMaster.html#aee2fccb9a95dd83c89049c86a86ff5f3">_fd</a>) &lt;= 0)</div>
<div class="line"><a id="l00040" name="l00040"></a><span class="lineno"> 40</span> <span class="keywordflow">throw</span> std::runtime_error(<span class="stringliteral">&quot;bind() error: &quot;</span> + <span class="keywordtype">string</span>(strerror(errno)));</div>
<div class="line"><a id="l00041" name="l00041"></a><span class="lineno"> 41</span> <span class="keywordflow">if</span> (listen(<a class="code hl_variable" href="classMaster.html#aee2fccb9a95dd83c89049c86a86ff5f3">_fd</a>, 3) &lt; 0 &amp;&amp; close(<a class="code hl_variable" href="classMaster.html#aee2fccb9a95dd83c89049c86a86ff5f3">_fd</a>) &lt;= 0)</div>
<div class="line"><a id="l00042" name="l00042"></a><span class="lineno"> 42</span> <span class="keywordflow">throw</span> std::runtime_error(<span class="stringliteral">&quot;listen() error: &quot;</span> + <span class="keywordtype">string</span>(strerror(errno)));</div>
<div class="line"><a id="l00043" name="l00043"></a><span class="lineno"> 43</span> cout &lt;&lt; <span class="stringliteral">&quot;New master socket with fd &quot;</span> &lt;&lt; <a class="code hl_variable" href="classMaster.html#aee2fccb9a95dd83c89049c86a86ff5f3">_fd</a> &lt;&lt; <span class="stringliteral">&quot; which listen &quot;</span> &lt;&lt; ip &lt;&lt; <span class="stringliteral">&quot;:&quot;</span> &lt;&lt; port &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div>
<div class="line"><a id="l00044" name="l00044"></a><span class="lineno"> 44</span> <span class="keywordflow">if</span> (<a class="code hl_variable" href="classMaster.html#aee2fccb9a95dd83c89049c86a86ff5f3">_fd</a> &lt; <a class="code hl_variable" href="classMaster.html#a6c0dd4e4c7de17dea3de77f28e1de308">_min_fd</a>)</div>
<div class="line"><a id="l00045" name="l00045"></a><span class="lineno"> 45</span> <a class="code hl_variable" href="classMaster.html#a6c0dd4e4c7de17dea3de77f28e1de308">_min_fd</a> = <a class="code hl_variable" href="classMaster.html#aee2fccb9a95dd83c89049c86a86ff5f3">_fd</a>;</div>
<div class="line"><a id="l00046" name="l00046"></a><span class="lineno"> 46</span> <a class="code hl_variable" href="classMaster.html#a2418abd3c0d0e9f4db769335cadd7545">_amount</a>++;</div>
<div class="line"><a id="l00047" name="l00047"></a><span class="lineno"> 47</span>}</div>
<div class="line"><a id="l00048" name="l00048"></a><span class="lineno"> 48</span><span class="comment">/* Set into static Master::readfds the active fds which will be select.*/</span></div>
<div class="line"><a id="l00049" name="l00049"></a><span class="lineno"> 49</span> </div>
<div class="line"><a id="l00050" name="l00050"></a><span class="lineno"><a class="line" href="classMaster.html#aa129a09fad260366b16aa04a7de173c2"> 50</a></span><span class="keywordtype">void</span> <a class="code hl_function" href="classMaster.html#aa129a09fad260366b16aa04a7de173c2">Master::pre_select</a>(<span class="keywordtype">void</span>) {</div>
<div class="line"><a id="l00051" name="l00051"></a><span class="lineno"> 51</span> FD_SET(<a class="code hl_variable" href="classMaster.html#aee2fccb9a95dd83c89049c86a86ff5f3">_fd</a>, &amp;<a class="code hl_variable" href="classMaster.html#a16a05173b1961ea7fc302319f06716a4">_readfds</a>);</div>
<div class="line"><a id="l00052" name="l00052"></a><span class="lineno"> 52</span> <span class="keywordflow">for</span> (std::vector&lt;Client *&gt;::iterator child = <a class="code hl_variable" href="classMaster.html#af4afc9ab34343b13939139087e280472">_childs</a>.begin(); child &lt; <a class="code hl_variable" href="classMaster.html#af4afc9ab34343b13939139087e280472">_childs</a>.end(); child++) {</div>
<div class="line"><a id="l00053" name="l00053"></a><span class="lineno"> 53</span> FD_SET((*child)-&gt;_fd, &amp;<a class="code hl_variable" href="classMaster.html#a16a05173b1961ea7fc302319f06716a4">_readfds</a>);</div>
<div class="line"><a id="l00054" name="l00054"></a><span class="lineno"> 54</span> <span class="keywordflow">if</span> ((*child)-&gt;_fd &gt; <a class="code hl_variable" href="classMaster.html#a40b24ae083809951e7d3e1a08d06fe65">_max_fd</a>)</div>
<div class="line"><a id="l00055" name="l00055"></a><span class="lineno"> 55</span> <a class="code hl_variable" href="classMaster.html#a40b24ae083809951e7d3e1a08d06fe65">_max_fd</a> = (*child)-&gt;_fd;</div>
<div class="line"><a id="l00056" name="l00056"></a><span class="lineno"> 56</span> }</div>
<div class="line"><a id="l00057" name="l00057"></a><span class="lineno"> 57</span>}</div>
<div class="line"><a id="l00058" name="l00058"></a><span class="lineno"> 58</span><span class="comment">/* |==========|</span></div>
<div class="line"><a id="l00059" name="l00059"></a><span class="lineno"> 59</span><span class="comment"> * Refresh master socket datas after select()</span></div>
<div class="line"><a id="l00060" name="l00060"></a><span class="lineno"> 60</span><span class="comment"> * - look first for new clients</span></div>
<div class="line"><a id="l00061" name="l00061"></a><span class="lineno"> 61</span><span class="comment"> * - look then if known clients sent requests or disconnected</span></div>
<div class="line"><a id="l00062" name="l00062"></a><span class="lineno"> 62</span><span class="comment"> * - if client sent request, handle it to generate answer adapted</span></div>
<div class="line"><a id="l00063" name="l00063"></a><span class="lineno"> 63</span><span class="comment"> */</span></div>
<div class="line"><a id="l00064" name="l00064"></a><span class="lineno"> 64</span> </div>
<div class="line"><a id="l00065" name="l00065"></a><span class="lineno"><a class="line" href="classMaster.html#a3ffb0ce7702a04ab7a9ec5dfa1ff6e42"> 65</a></span><span class="keywordtype">void</span> <a class="code hl_function" href="classMaster.html#a3ffb0ce7702a04ab7a9ec5dfa1ff6e42">Master::post_select</a>(<a class="code hl_class" href="classEnv.html">Env</a> *env) {</div>
<div class="line"><a id="l00066" name="l00066"></a><span class="lineno"> 66</span> <span class="keywordtype">int</span> valread;</div>
<div class="line"><a id="l00067" name="l00067"></a><span class="lineno"> 67</span> <span class="keywordtype">int</span> addrlen = <span class="keyword">sizeof</span>(<a class="code hl_variable" href="classMaster.html#a2930920309de02e86050a8facdc83b68">_address</a>);</div>
<div class="line"><a id="l00068" name="l00068"></a><span class="lineno"> 68</span> <span class="keywordtype">char</span> buffer[128];</div>
<div class="line"><a id="l00069" name="l00069"></a><span class="lineno"> 69</span> </div>
<div class="line"><a id="l00070" name="l00070"></a><span class="lineno"> 70</span> <span class="keywordflow">if</span> (FD_ISSET(<a class="code hl_variable" href="classMaster.html#aee2fccb9a95dd83c89049c86a86ff5f3">_fd</a>, &amp;<a class="code hl_variable" href="classMaster.html#a16a05173b1961ea7fc302319f06716a4">_readfds</a>)) { </div>
<div class="line"><a id="l00071" name="l00071"></a><span class="lineno"> 71</span> <span class="keywordtype">int</span> new_socket = accept(<a class="code hl_variable" href="classMaster.html#aee2fccb9a95dd83c89049c86a86ff5f3">_fd</a>, (<span class="keyword">struct</span> sockaddr *)&amp;<a class="code hl_variable" href="classMaster.html#a2930920309de02e86050a8facdc83b68">_address</a>, (socklen_t *)&amp;addrlen);</div>
<div class="line"><a id="l00072" name="l00072"></a><span class="lineno"> 72</span> <span class="keywordflow">if</span> (new_socket &lt; 0)</div>
<div class="line"><a id="l00073" name="l00073"></a><span class="lineno"> 73</span> <span class="keywordflow">throw</span> std::runtime_error(<span class="stringliteral">&quot;accept() error:&quot;</span> + <span class="keywordtype">string</span>(strerror(errno)));</div>
<div class="line"><a id="l00074" name="l00074"></a><span class="lineno"> 74</span><span class="preprocessor">#ifdef __APPLE__</span></div>
<div class="line"><a id="l00075" name="l00075"></a><span class="lineno"> 75</span> fcntl(new_socket, F_SETFL, O_NONBLOCK);</div>
<div class="line"><a id="l00076" name="l00076"></a><span class="lineno"> 76</span><span class="preprocessor">#endif</span></div>
<div class="line"><a id="l00077" name="l00077"></a><span class="lineno"> 77</span> <a class="code hl_struct" href="structip__port__s.html">ip_port_t</a> cli_listen = <a class="code hl_function" href="tools_8cpp.html#a6e7e23c21ae956b6859a6f3ec9793358">get_ip_port_t</a>(inet_ntoa(<a class="code hl_variable" href="classMaster.html#a2930920309de02e86050a8facdc83b68">_address</a>.sin_addr), ntohs(<a class="code hl_variable" href="classMaster.html#a2930920309de02e86050a8facdc83b68">_address</a>.sin_port));</div>
<div class="line"><a id="l00078" name="l00078"></a><span class="lineno"> 78</span> <a class="code hl_variable" href="classMaster.html#af4afc9ab34343b13939139087e280472">_childs</a>.push_back(<span class="keyword">new</span> <a class="code hl_class" href="classClient.html">Client</a>(new_socket, cli_listen, <span class="keyword">this</span>));</div>
<div class="line"><a id="l00079" name="l00079"></a><span class="lineno"> 79</span> <a class="code hl_variable" href="classMaster.html#a2418abd3c0d0e9f4db769335cadd7545">_amount</a>++;</div>
<div class="line"><a id="l00080" name="l00080"></a><span class="lineno"> 80</span> }</div>
<div class="line"><a id="l00081" name="l00081"></a><span class="lineno"> 81</span> <span class="keywordtype">int</span> child_fd;</div>
<div class="line"><a id="l00082" name="l00082"></a><span class="lineno"> 82</span> <span class="keywordflow">for</span> (std::vector&lt;Client *&gt;::iterator it = <a class="code hl_variable" href="classMaster.html#af4afc9ab34343b13939139087e280472">_childs</a>.begin(); it &lt; <a class="code hl_variable" href="classMaster.html#af4afc9ab34343b13939139087e280472">_childs</a>.end(); it++) {</div>
<div class="line"><a id="l00083" name="l00083"></a><span class="lineno"> 83</span> child_fd = (*it)-&gt;_fd;</div>
<div class="line"><a id="l00084" name="l00084"></a><span class="lineno"> 84</span> <span class="keywordflow">if</span> (FD_ISSET(child_fd, &amp;<a class="code hl_variable" href="classMaster.html#a16a05173b1961ea7fc302319f06716a4">_readfds</a>)) {</div>
<div class="line"><a id="l00085" name="l00085"></a><span class="lineno"> 85</span> valread = read(child_fd, buffer, 128);</div>
<div class="line"><a id="l00086" name="l00086"></a><span class="lineno"> 86</span> buffer[valread] = <span class="charliteral">&#39;\0&#39;</span>;</div>
<div class="line"><a id="l00087" name="l00087"></a><span class="lineno"> 87</span> <span class="keywordflow">if</span> (valread == 0) {</div>
<div class="line"><a id="l00088" name="l00088"></a><span class="lineno"> 88</span> getpeername(child_fd, (<span class="keyword">struct</span> sockaddr *)&amp;<a class="code hl_variable" href="classMaster.html#a2930920309de02e86050a8facdc83b68">_address</a>, (socklen_t *)&amp;addrlen);</div>
<div class="line"><a id="l00089" name="l00089"></a><span class="lineno"> 89</span> <span class="keyword">delete</span> (*it);</div>
<div class="line"><a id="l00090" name="l00090"></a><span class="lineno"> 90</span> <a class="code hl_variable" href="classMaster.html#af4afc9ab34343b13939139087e280472">_childs</a>.erase(it);</div>
<div class="line"><a id="l00091" name="l00091"></a><span class="lineno"> 91</span> <a class="code hl_variable" href="classMaster.html#a2418abd3c0d0e9f4db769335cadd7545">_amount</a>--;</div>
<div class="line"><a id="l00092" name="l00092"></a><span class="lineno"> 92</span> } <span class="keywordflow">else</span> <span class="keywordflow">if</span> ((*it)-&gt;getRequest(env, buffer))</div>
<div class="line"><a id="l00093" name="l00093"></a><span class="lineno"> 93</span> (*it)-&gt;handleRequest();</div>
<div class="line"><a id="l00094" name="l00094"></a><span class="lineno"> 94</span> }</div>
<div class="line"><a id="l00095" name="l00095"></a><span class="lineno"> 95</span> }</div>
<div class="line"><a id="l00096" name="l00096"></a><span class="lineno"> 96</span>}</div>
<div class="line"><a id="l00097" name="l00097"></a><span class="lineno"> 97</span><span class="comment">/* |==========|</span></div>
<div class="line"><a id="l00098" name="l00098"></a><span class="lineno"> 98</span><span class="comment"> * Choose the server which must handle a request</span></div>
<div class="line"><a id="l00099" name="l00099"></a><span class="lineno"> 99</span><span class="comment"> * Each server can listen multiple range_ip:port and each range_ip:port can be</span></div>
<div class="line"><a id="l00100" name="l00100"></a><span class="lineno"> 100</span><span class="comment"> * listen by multiple servers. So for each request, we must look at the socket</span></div>
<div class="line"><a id="l00101" name="l00101"></a><span class="lineno"> 101</span><span class="comment"> * which given us the client to know how the client came. If multiple servers</span></div>
<div class="line"><a id="l00102" name="l00102"></a><span class="lineno"> 102</span><span class="comment"> * listen the range from where the client came, ones with exact correspondance</span></div>
<div class="line"><a id="l00103" name="l00103"></a><span class="lineno"> 103</span><span class="comment"> * are prefered.</span></div>
<div class="line"><a id="l00104" name="l00104"></a><span class="lineno"> 104</span><span class="comment"> *</span></div>
<div class="line"><a id="l00105" name="l00105"></a><span class="lineno"> 105</span><span class="comment"> * If there are multiples servers listening exactly the ip the client try to</span></div>
<div class="line"><a id="l00106" name="l00106"></a><span class="lineno"> 106</span><span class="comment"> * reach or whic listen a range which contain it, the first one which have the</span></div>
<div class="line"><a id="l00107" name="l00107"></a><span class="lineno"> 107</span><span class="comment"> * same server_name as the host the client used to reach server is used, else</span></div>
<div class="line"><a id="l00108" name="l00108"></a><span class="lineno"> 108</span><span class="comment"> * it&#39;s the first one of exact correspondance or first one which have the ip</span></div>
<div class="line"><a id="l00109" name="l00109"></a><span class="lineno"> 109</span><span class="comment"> * requested in his listen range.</span></div>
<div class="line"><a id="l00110" name="l00110"></a><span class="lineno"> 110</span><span class="comment"> *</span></div>
<div class="line"><a id="l00111" name="l00111"></a><span class="lineno"> 111</span><span class="comment"> */</span></div>
<div class="line"><a id="l00112" name="l00112"></a><span class="lineno"> 112</span> </div>
<div class="line"><a id="l00113" name="l00113"></a><span class="lineno"><a class="line" href="classMaster.html#a5486cb0d32ba29cd31b212c3457942b1"> 113</a></span><a class="code hl_class" href="classServer.html">Server</a> *<a class="code hl_function" href="classMaster.html#a5486cb0d32ba29cd31b212c3457942b1">Master::choose_server</a>(<a class="code hl_class" href="classEnv.html">Env</a> *env, <span class="keywordtype">string</span> host) {</div>
<div class="line"><a id="l00114" name="l00114"></a><span class="lineno"> 114</span> std::vector&lt;Server *&gt; exact, inrange;</div>
<div class="line"><a id="l00115" name="l00115"></a><span class="lineno"> 115</span> <a class="code hl_typedef" href="webserv_8hpp.html#aba96a6988a5bc02b5c8b908af07d0bbb">vec_string</a> ip_listen, ip_required;</div>
<div class="line"><a id="l00116" name="l00116"></a><span class="lineno"> 116</span> </div>
<div class="line"><a id="l00117" name="l00117"></a><span class="lineno"> 117</span> ip_required = <a class="code hl_function" href="tools_8cpp.html#a4a79e98b5dec14d16875307f7e77dfb3">split</a>(<a class="code hl_variable" href="classMaster.html#af5432a935125075c1eb03aa9e8c8f894">_listen</a>.<a class="code hl_variable" href="structip__port__s.html#afb20776a9723a50ac32fd9b8248b3e0c">ip</a>, <span class="stringliteral">&quot;.&quot;</span>);</div>
<div class="line"><a id="l00118" name="l00118"></a><span class="lineno"> 118</span> <span class="keywordflow">for</span> (std::vector&lt;Server *&gt;::iterator server = env-&gt;<a class="code hl_variable" href="classEnv.html#ae82dda3aafec12c1b84c55227d606304">_servers</a>.begin(); server &lt; env-&gt;_servers.end(); server++) {</div>
<div class="line"><a id="l00119" name="l00119"></a><span class="lineno"> 119</span> std::vector&lt;ip_port_t&gt; serv_listens = (*server)-&gt;_listens;</div>
<div class="line"><a id="l00120" name="l00120"></a><span class="lineno"> 120</span> <span class="keywordflow">for</span> (std::vector&lt;ip_port_t&gt;::iterator it = serv_listens.begin(); it &lt; serv_listens.end(); it++) {</div>
<div class="line"><a id="l00121" name="l00121"></a><span class="lineno"> 121</span> <span class="keywordflow">if</span> (<a class="code hl_variable" href="classMaster.html#af5432a935125075c1eb03aa9e8c8f894">_listen</a>.<a class="code hl_variable" href="structip__port__s.html#aae56f9ac0aa0193aaae11be127aa578e">port</a> != (*it).port)</div>
<div class="line"><a id="l00122" name="l00122"></a><span class="lineno"> 122</span> <span class="keywordflow">continue</span>;</div>
<div class="line"><a id="l00123" name="l00123"></a><span class="lineno"> 123</span> <span class="keywordflow">if</span> (<a class="code hl_variable" href="classMaster.html#af5432a935125075c1eb03aa9e8c8f894">_listen</a>.<a class="code hl_variable" href="structip__port__s.html#afb20776a9723a50ac32fd9b8248b3e0c">ip</a> == (*it).ip) {</div>
<div class="line"><a id="l00124" name="l00124"></a><span class="lineno"> 124</span> exact.push_back(*server);</div>
<div class="line"><a id="l00125" name="l00125"></a><span class="lineno"> 125</span> <span class="keywordflow">continue</span>;</div>
<div class="line"><a id="l00126" name="l00126"></a><span class="lineno"> 126</span> }</div>
<div class="line"><a id="l00127" name="l00127"></a><span class="lineno"> 127</span> <span class="keywordtype">bool</span> is_inrange = <span class="keyword">true</span>;</div>
<div class="line"><a id="l00128" name="l00128"></a><span class="lineno"> 128</span> ip_listen = <a class="code hl_function" href="tools_8cpp.html#a4a79e98b5dec14d16875307f7e77dfb3">split</a>((*it).ip, <span class="stringliteral">&quot;.&quot;</span>);</div>
<div class="line"><a id="l00129" name="l00129"></a><span class="lineno"> 129</span> vec_string::iterator r = ip_required.begin();</div>
<div class="line"><a id="l00130" name="l00130"></a><span class="lineno"> 130</span> <span class="keywordflow">for</span> (vec_string::iterator l = ip_listen.end(); l &gt;= ip_listen.begin(); --l) {</div>
<div class="line"><a id="l00131" name="l00131"></a><span class="lineno"> 131</span> <span class="keywordflow">if</span> (*l != *r &amp;&amp; *l != <span class="stringliteral">&quot;0&quot;</span>)</div>
<div class="line"><a id="l00132" name="l00132"></a><span class="lineno"> 132</span> is_inrange = <span class="keyword">false</span>;</div>
<div class="line"><a id="l00133" name="l00133"></a><span class="lineno"> 133</span> }</div>
<div class="line"><a id="l00134" name="l00134"></a><span class="lineno"> 134</span> <span class="keywordflow">if</span> (is_inrange)</div>
<div class="line"><a id="l00135" name="l00135"></a><span class="lineno"> 135</span> inrange.push_back(*server);</div>
<div class="line"><a id="l00136" name="l00136"></a><span class="lineno"> 136</span> }</div>
<div class="line"><a id="l00137" name="l00137"></a><span class="lineno"> 137</span> }</div>
<div class="line"><a id="l00138" name="l00138"></a><span class="lineno"> 138</span> <span class="keywordflow">if</span> (<a class="code hl_define" href="webserv_8hpp.html#ad72dbcf6d0153db1b8d8a58001feed83">DEBUG</a>)</div>
<div class="line"><a id="l00139" name="l00139"></a><span class="lineno"> 139</span> std::cout &lt;&lt; <span class="stringliteral">&quot;req: &quot;</span> &lt;&lt; <a class="code hl_variable" href="classMaster.html#af5432a935125075c1eb03aa9e8c8f894">_listen</a>.<a class="code hl_variable" href="structip__port__s.html#afb20776a9723a50ac32fd9b8248b3e0c">ip</a> &lt;&lt; <span class="stringliteral">&quot;:&quot;</span> &lt;&lt; <a class="code hl_variable" href="classMaster.html#af5432a935125075c1eb03aa9e8c8f894">_listen</a>.<a class="code hl_variable" href="structip__port__s.html#aae56f9ac0aa0193aaae11be127aa578e">port</a> &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div>
<div class="line"><a id="l00140" name="l00140"></a><span class="lineno"> 140</span> <span class="keywordflow">if</span> (exact.size() == 0) {</div>
<div class="line"><a id="l00141" name="l00141"></a><span class="lineno"> 141</span> std::cout &lt;&lt; <span class="stringliteral">&quot;in range server check\n&quot;</span>;</div>
<div class="line"><a id="l00142" name="l00142"></a><span class="lineno"> 142</span> <span class="keywordflow">for</span> (std::vector&lt;Server *&gt;::iterator server = inrange.begin(); server &lt; inrange.end(); server++) {</div>
<div class="line"><a id="l00143" name="l00143"></a><span class="lineno"> 143</span> <span class="keywordflow">if</span> (host == (*server)-&gt;getName())</div>
<div class="line"><a id="l00144" name="l00144"></a><span class="lineno"> 144</span> <span class="keywordflow">return</span> *server;</div>
<div class="line"><a id="l00145" name="l00145"></a><span class="lineno"> 145</span> }</div>
<div class="line"><a id="l00146" name="l00146"></a><span class="lineno"> 146</span> <span class="keywordflow">return</span> inrange.front();</div>
<div class="line"><a id="l00147" name="l00147"></a><span class="lineno"> 147</span> } <span class="keywordflow">else</span> {</div>
<div class="line"><a id="l00148" name="l00148"></a><span class="lineno"> 148</span> std::cout &lt;&lt; <span class="stringliteral">&quot;exact server check\n&quot;</span>;</div>
<div class="line"><a id="l00149" name="l00149"></a><span class="lineno"> 149</span> <span class="keywordflow">for</span> (std::vector&lt;Server *&gt;::iterator server = exact.begin(); server &lt; exact.end(); server++) {</div>
<div class="line"><a id="l00150" name="l00150"></a><span class="lineno"> 150</span> <span class="keywordflow">if</span> (host == (*server)-&gt;getName())</div>
<div class="line"><a id="l00151" name="l00151"></a><span class="lineno"> 151</span> <span class="keywordflow">return</span> *server;</div>
<div class="line"><a id="l00152" name="l00152"></a><span class="lineno"> 152</span> }</div>
<div class="line"><a id="l00153" name="l00153"></a><span class="lineno"> 153</span> <span class="keywordflow">return</span> exact.front();</div>
<div class="line"><a id="l00154" name="l00154"></a><span class="lineno"> 154</span> }</div>
<div class="line"><a id="l00155" name="l00155"></a><span class="lineno"> 155</span>}</div>
<div class="ttc" id="aclassClient_html"><div class="ttname"><a href="classClient.html">Client</a></div><div class="ttdef"><b>Definition:</b> <a href="Client_8hpp_source.html#l00004">Client.hpp:4</a></div></div>
<div class="ttc" id="aclassEnv_html"><div class="ttname"><a href="classEnv.html">Env</a></div><div class="ttdef"><b>Definition:</b> <a href="Env_8hpp_source.html#l00004">Env.hpp:4</a></div></div>
<div class="ttc" id="aclassEnv_html_ae82dda3aafec12c1b84c55227d606304"><div class="ttname"><a href="classEnv.html#ae82dda3aafec12c1b84c55227d606304">Env::_servers</a></div><div class="ttdeci">std::vector&lt; Server * &gt; _servers</div><div class="ttdef"><b>Definition:</b> <a href="Env_8hpp_source.html#l00006">Env.hpp:6</a></div></div>
<div class="ttc" id="aclassMaster_html_a16a05173b1961ea7fc302319f06716a4"><div class="ttname"><a href="classMaster.html#a16a05173b1961ea7fc302319f06716a4">Master::_readfds</a></div><div class="ttdeci">static fd_set _readfds</div><div class="ttdef"><b>Definition:</b> <a href="Master_8hpp_source.html#l00019">Master.hpp:19</a></div></div>
<div class="ttc" id="aclassMaster_html_a2418abd3c0d0e9f4db769335cadd7545"><div class="ttname"><a href="classMaster.html#a2418abd3c0d0e9f4db769335cadd7545">Master::_amount</a></div><div class="ttdeci">static int _amount</div><div class="ttdoc">&lt; The higher one</div><div class="ttdef"><b>Definition:</b> <a href="Master_8hpp_source.html#l00020">Master.hpp:20</a></div></div>
<div class="ttc" id="aclassMaster_html_a2930920309de02e86050a8facdc83b68"><div class="ttname"><a href="classMaster.html#a2930920309de02e86050a8facdc83b68">Master::_address</a></div><div class="ttdeci">struct sockaddr_in _address</div><div class="ttdef"><b>Definition:</b> <a href="Master_8hpp_source.html#l00007">Master.hpp:7</a></div></div>
<div class="ttc" id="aclassMaster_html_a3ffb0ce7702a04ab7a9ec5dfa1ff6e42"><div class="ttname"><a href="classMaster.html#a3ffb0ce7702a04ab7a9ec5dfa1ff6e42">Master::post_select</a></div><div class="ttdeci">void post_select(Env *env)</div><div class="ttdef"><b>Definition:</b> <a href="Master_8cpp_source.html#l00065">Master.cpp:65</a></div></div>
<div class="ttc" id="aclassMaster_html_a40b24ae083809951e7d3e1a08d06fe65"><div class="ttname"><a href="classMaster.html#a40b24ae083809951e7d3e1a08d06fe65">Master::_max_fd</a></div><div class="ttdeci">static int _max_fd</div><div class="ttdoc">&lt; The lower socket fd</div><div class="ttdef"><b>Definition:</b> <a href="Master_8hpp_source.html#l00020">Master.hpp:20</a></div></div>
<div class="ttc" id="aclassMaster_html_a5486cb0d32ba29cd31b212c3457942b1"><div class="ttname"><a href="classMaster.html#a5486cb0d32ba29cd31b212c3457942b1">Master::choose_server</a></div><div class="ttdeci">Server * choose_server(Env *env, string host)</div><div class="ttdef"><b>Definition:</b> <a href="Master_8cpp_source.html#l00113">Master.cpp:113</a></div></div>
<div class="ttc" id="aclassMaster_html_a6c0dd4e4c7de17dea3de77f28e1de308"><div class="ttname"><a href="classMaster.html#a6c0dd4e4c7de17dea3de77f28e1de308">Master::_min_fd</a></div><div class="ttdeci">static int _min_fd</div><div class="ttdoc">&lt; The sockets fd which will be select</div><div class="ttdef"><b>Definition:</b> <a href="Master_8hpp_source.html#l00020">Master.hpp:20</a></div></div>
<div class="ttc" id="aclassMaster_html_aa129a09fad260366b16aa04a7de173c2"><div class="ttname"><a href="classMaster.html#aa129a09fad260366b16aa04a7de173c2">Master::pre_select</a></div><div class="ttdeci">void pre_select(void)</div><div class="ttdef"><b>Definition:</b> <a href="Master_8cpp_source.html#l00050">Master.cpp:50</a></div></div>
<div class="ttc" id="aclassMaster_html_aa32552c9c965ac5d53d2cb5e65b41b36"><div class="ttname"><a href="classMaster.html#aa32552c9c965ac5d53d2cb5e65b41b36">Master::Master</a></div><div class="ttdeci">Master(ip_port_t listen)</div><div class="ttdef"><b>Definition:</b> <a href="Master_8cpp_source.html#l00025">Master.cpp:25</a></div></div>
<div class="ttc" id="aclassMaster_html_aa76c335c36c93e8134115733938651df"><div class="ttname"><a href="classMaster.html#aa76c335c36c93e8134115733938651df">Master::~Master</a></div><div class="ttdeci">~Master(void)</div><div class="ttdef"><b>Definition:</b> <a href="Master_8cpp_source.html#l00011">Master.cpp:11</a></div></div>
<div class="ttc" id="aclassMaster_html_aee2fccb9a95dd83c89049c86a86ff5f3"><div class="ttname"><a href="classMaster.html#aee2fccb9a95dd83c89049c86a86ff5f3">Master::_fd</a></div><div class="ttdeci">int _fd</div><div class="ttdef"><b>Definition:</b> <a href="Master_8hpp_source.html#l00005">Master.hpp:5</a></div></div>
<div class="ttc" id="aclassMaster_html_af4afc9ab34343b13939139087e280472"><div class="ttname"><a href="classMaster.html#af4afc9ab34343b13939139087e280472">Master::_childs</a></div><div class="ttdeci">std::vector&lt; Client * &gt; _childs</div><div class="ttdef"><b>Definition:</b> <a href="Master_8hpp_source.html#l00006">Master.hpp:6</a></div></div>
<div class="ttc" id="aclassMaster_html_af5432a935125075c1eb03aa9e8c8f894"><div class="ttname"><a href="classMaster.html#af5432a935125075c1eb03aa9e8c8f894">Master::_listen</a></div><div class="ttdeci">ip_port_t _listen</div><div class="ttdef"><b>Definition:</b> <a href="Master_8hpp_source.html#l00018">Master.hpp:18</a></div></div>
<div class="ttc" id="aclassServer_html"><div class="ttname"><a href="classServer.html">Server</a></div><div class="ttdef"><b>Definition:</b> <a href="Server_8hpp_source.html#l00004">Server.hpp:4</a></div></div>
<div class="ttc" id="astructip__port__s_html"><div class="ttname"><a href="structip__port__s.html">ip_port_s</a></div><div class="ttdef"><b>Definition:</b> <a href="webserv_8hpp_source.html#l00035">webserv.hpp:35</a></div></div>
<div class="ttc" id="astructip__port__s_html_aae56f9ac0aa0193aaae11be127aa578e"><div class="ttname"><a href="structip__port__s.html#aae56f9ac0aa0193aaae11be127aa578e">ip_port_s::port</a></div><div class="ttdeci">int port</div><div class="ttdef"><b>Definition:</b> <a href="webserv_8hpp_source.html#l00038">webserv.hpp:38</a></div></div>
<div class="ttc" id="astructip__port__s_html_afb20776a9723a50ac32fd9b8248b3e0c"><div class="ttname"><a href="structip__port__s.html#afb20776a9723a50ac32fd9b8248b3e0c">ip_port_s::ip</a></div><div class="ttdeci">string ip</div><div class="ttdef"><b>Definition:</b> <a href="webserv_8hpp_source.html#l00037">webserv.hpp:37</a></div></div>
<div class="ttc" id="atools_8cpp_html_a4a79e98b5dec14d16875307f7e77dfb3"><div class="ttname"><a href="tools_8cpp.html#a4a79e98b5dec14d16875307f7e77dfb3">split</a></div><div class="ttdeci">vec_string split(string str, string delim)</div><div class="ttdef"><b>Definition:</b> <a href="tools_8cpp_source.html#l00020">tools.cpp:20</a></div></div>
<div class="ttc" id="atools_8cpp_html_a6e7e23c21ae956b6859a6f3ec9793358"><div class="ttname"><a href="tools_8cpp.html#a6e7e23c21ae956b6859a6f3ec9793358">get_ip_port_t</a></div><div class="ttdeci">ip_port_t get_ip_port_t(string listen)</div><div class="ttdef"><b>Definition:</b> <a href="tools_8cpp_source.html#l00035">tools.cpp:35</a></div></div>
<div class="ttc" id="awebserv_8hpp_html"><div class="ttname"><a href="webserv_8hpp.html">webserv.hpp</a></div></div>
<div class="ttc" id="awebserv_8hpp_html_aba96a6988a5bc02b5c8b908af07d0bbb"><div class="ttname"><a href="webserv_8hpp.html#aba96a6988a5bc02b5c8b908af07d0bbb">vec_string</a></div><div class="ttdeci">std::vector&lt; string &gt; vec_string</div><div class="ttdef"><b>Definition:</b> <a href="webserv_8hpp_source.html#l00050">webserv.hpp:50</a></div></div>
<div class="ttc" id="awebserv_8hpp_html_ad72dbcf6d0153db1b8d8a58001feed83"><div class="ttname"><a href="webserv_8hpp.html#ad72dbcf6d0153db1b8d8a58001feed83">DEBUG</a></div><div class="ttdeci">#define DEBUG</div><div class="ttdef"><b>Definition:</b> <a href="webserv_8hpp_source.html#l00002">webserv.hpp:2</a></div></div>
</div><!-- fragment --></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by&#160;<a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.7
</small></address>
</body>
</html>