Closed form (or an ODE) for the integral $\int_0^\infty \frac{1+z^2}{1+z^4} \frac{z^p}{1+z^{2p}} dz$

The task contains the rational parameter $p$, which can be integer or irreducible fraction.

$$\color{brown}{\textbf{Integer parameter $p$.}}$$

$$I(p) = \int\limits_0^\infty\dfrac{1+z^2}{1+z^4}\dfrac{z^p}{1+z^{2p}} \,\mathrm dz.\tag1$$

If parameter $p$ is even, then the residue theorem can be applied immediately.

However, the case $p=4q+2$ requires separate consideration, due to the multiple poles.

If parameter $p$ is odd, then the function under the integral can be presented as the sum of the simple terms.

$\color{blue}{\textbf{Case $p=4q$.}}$

Using residue theorem, one can get \begin{align} &I(p) = \dfrac12\int\limits_{-\infty}^\infty\dfrac{1+z^2}{1+z^4}\dfrac{z^p}{1+z^{2p}}\,\mathrm dz = \pi i \sum\limits_{\large{k, \Im z_k>0}}\mathop{\mathrm{Res}}\limits_{\large{z_k}}\dfrac{1+z^2}{1+z^4}\dfrac{z^{4q}}{1+z^{8q}}\\ & = \pi i\left(\mathop{\mathrm{Res}}\limits_{\large{e^{\frac14\pi i}}}\dfrac{1+z^2}{1+z^4}\dfrac{z^{4q}}{1+z^{8q}} + \mathop{\mathrm{Res}}\limits_{\large{e^{\frac34\pi i}}}\dfrac{1+z^2}{1+z^4}\dfrac{z^{4q}}{1+z^{8q}} + \sum\limits_{\large{k=0}}^{\large{4q-1}}\ \mathop{\mathrm{Res}}\limits_{\large{e^{\frac{2k+1}{8q}\pi i}}}\dfrac{1+z^2}{1+z^4}\dfrac{z^{4q}}{1+z^{8q}}\right)\\ & = \pi i\left(\lim\limits_{\large{z\to e^{\frac14\pi i}}}\dfrac{1+z^2}{4z^3}\dfrac{z^{4q}}{1+z^{8q}} + \lim\limits_{\large{z\to e^{\frac34\pi i}}}\dfrac{1+z^2}{4z^3}\dfrac{z^{4q}}{1+z^{8q}} + \sum\limits_{\large{k=0}}^{\large{4q-1}}\ \lim\limits_{\large{z\to e^{\frac{2k+1}{8q}\pi i}}}\dfrac{1+z^2}{1+z^4}\dfrac{z}{8qz^{4q}}\right)\\ & = \pi i\left(\dfrac{1+i}{4e^{\frac34\pi i}}\dfrac{(-1)^q}2 + \dfrac{1-i}{4e^{\frac14\pi i}}\dfrac{(-1)^q}2 + \sum\limits_{\large{k=0}}^{\large{4q-1}}\ \dfrac{1+e^{\frac{2k+1}{4q}\pi i}}{1+e^{\frac{2k+1}{2q}\pi i}}\dfrac{e^{\frac{2k+1}{8q}\pi i}}{8qe^{\frac{2k+1}2\pi i}}\right)\\ & = \pi i\left(-\dfrac{(-1)^q\sqrt2}8i - \dfrac{(-1)^q\sqrt2}8i - \dfrac{i}{8q}\sum\limits_{\large{k=0}}^{\large{4q-1}}(-1)^k \dfrac{1+e^{\frac{2k+1}{4q}\pi i}}{e^{\frac{2k+1}{8q}\pi i}}\dfrac{e^{\frac{2k+1}{4q}\pi i}}{1+e^{\frac{2k+1}{2q}\pi i}}\right),\\ &I(4q) = \dfrac\pi4 \left((-1)^q\sqrt2 + \dfrac1{2q}\sum\limits_{\large{k=0}}^{\large{4q-1}}(-1)^k\dfrac{\cos\frac{2k+1}{8q}\pi}{\cos\frac{2k+1}{4q}\pi}\right).\tag2\\ \end{align} Formula $(2)$ can be simplified by grouping of the symmetric terms, then \begin{align} &I(4q) = \dfrac\pi4 \left((-1)^q\sqrt2 + \dfrac1{2q}\sum\limits_{\large{k=0}}^{\large{2q-1}}(-1)^k\left(\dfrac{\cos\frac{2k+1}{8q}\pi}{\cos\frac{2k+1}{4q}\pi} - \dfrac{\cos\frac{2(4q-k-1)+1}{8q}\pi}{\cos\frac{2(4q-k-1)+1}{4q}\pi}\right)\right)\\ &= \dfrac\pi4 \left((-1)^q\sqrt2 + \dfrac1{q}\sum\limits_{\large{k=0}}^{\large{2q-1}}(-1)^k \dfrac{\cos\frac{2k+1}{8q}\pi}{\cos\frac{2k+1}{4q}\pi}\right)\\ &= \dfrac\pi4 \left((-1)^q\sqrt2 + \dfrac1{q}\sum\limits_{\large{k=0}}^{\large{q-1}}(-1)^k\left( \dfrac{\cos\frac{2k+1}{8q}\pi}{\cos\frac{2k+1}{4q}\pi} - \dfrac{\cos\frac{2(2q-1-k)+1}{8q}\pi}{\cos\frac{2(2q-1-k)+1}{4q}\pi}\right)\right)\\ &= \dfrac\pi4 \left((-1)^q\sqrt2 + \dfrac1{q}\sum\limits_{\large{k=0}}^{\large{q-1}}(-1)^k \dfrac{\cos\frac{2k+1}{8q}\pi + \sin\frac{2k+1}{8q}\pi}{\cos\frac{2k+1}{4q}\pi}\right)\\ &= \dfrac\pi4 \left((-1)^q\sqrt2 + \dfrac{\sqrt2}{q}\sum\limits_{\large{k=0}}^{\large{q-1}}(-1)^k \dfrac{\sin\left(\frac\pi4 + \frac{2k+1}{8q}\pi\right)}{\sin\left(\frac\pi2+\frac{2k+1}{4q}\pi\right)}\right),\\ &\mathbf{\color{blue}{I(0) = \dfrac\pi{2\sqrt2}}},\tag3\\ &\mathbf{\color{blue}{I(4q) = \dfrac\pi{2q\sqrt2} \left((-1)^q\cdot q + \dfrac12\sum\limits_{\large{k=0}}^{\large{q-1}}(-1)^k \sec\frac{2q+2k+1}{8q}\pi\right),\quad q>0.}}\tag4\\ \end{align} The forms $(3)$ and $(4)$ are closed. Applying $(4),$ one can get the suitable alternative forms: \begin{align} &\mathbf{\color{green}{I(4) = \dfrac\pi{2\sqrt2}\left(\sqrt2\cos\frac\pi8-1\,\right) = \dfrac\pi4\left(\sqrt{2+\sqrt2}-\sqrt2\,\right)}},\\ &\mathbf{\color{green}{I(8) = \dfrac\pi{4\sqrt2}\left(2-\sqrt{2+\sqrt{2-\sqrt2}}\,\right)}},\\ &\mathbf{\color{green}{I(12) = \dfrac\pi{6\sqrt2}\left(\sqrt{7+\sqrt{\frac{49}2-4\sqrt3}}-3\right)}}\\ \end{align} (see also Wolfram Alpha).

If $q=4,$ then \begin{align} &\frac12\sum\limits_{\large{k=0}}^{\large{q-1}}(-1)^k \sec\frac{2q+2k+1}{8q}\pi =\frac1{2\cos\frac{9\pi}{32}} - \frac1{2\cos\frac{11\pi}{32}} + \frac1{2\cos\frac{13\pi}{32}} - \frac1{2\cos\frac{15\pi}{32}},\\[4pt] &\mathbf{\color{green}{I(16) = \dfrac\pi{8\sqrt2}\Bigg(4 + \frac1{\sqrt{2-\sqrt{2-\sqrt{2+\sqrt2}}}} - \frac1{\sqrt{2-\sqrt{2-\sqrt{2-\sqrt2}}}} + \frac1{\sqrt{2-\sqrt{2+\sqrt{2-\sqrt2}}}} - \frac1{\sqrt{2-\sqrt{2+\sqrt{2+\sqrt2}}}}\Bigg)}}\\ \end{align}

$\color{blue}{\textbf{Case $p=4q+2$.}}$

Similarly, using residue theorem and taking in account the multiple roots, one can get \begin{align} &I(p) = \dfrac12\int\limits_{-\infty}^\infty\dfrac{1+z^2}{1+z^4}\dfrac{z^p}{1+z^{2p}}\,\mathrm dz = \pi i \sum\limits_{\large{k, \Im z_k>0}}\mathop{\mathrm{Res}}\limits_{\large{z_k}}\dfrac{1+z^2}{1+z^4}\dfrac{z^{4q+2}}{1+z^{8q+4}}\\ & = \pi i\left(\mathop{\mathrm{Res}}\limits_{\large{e^{\frac14\pi i}}}\dfrac{1+z^2}{1+z^4}\dfrac{z^{4q+2}}{1+z^{8q+4}} + \mathop{\mathrm{Res}}\limits_{\large{e^{\frac34\pi i}}}\dfrac{1+z^2}{1+z^4}\dfrac{z^{4q+2}}{1+z^{8q+4}} + {\sum\limits_{\substack{k=0\dots q-1,\\q+1\dots3q,\\3q+2\dots4q+1}}\mathop{\mathrm{Res}} \limits_{\large{e^{\frac{2k+1}{8q+4}\pi i}}}\dfrac{1+z^2}{1+z^4}\dfrac{z^{4q+2}}{1+z^{8q+4}}}\right)\\ & = \pi i\left(\lim\limits_{\large{z\to e^{\frac14\pi i}}}\left(\dfrac{1+z^2}{1+z^4}\dfrac{z^{4q+2}\left(z-e^{\frac14\pi i}\right)^2}{1+z^{8q+4}}\right)' + \lim\limits_{\large{z\to e^{\frac34\pi i}}}\left(\dfrac{1+z^2}{1+z^4}\dfrac{z^{4q+2}\left(z-e^{\frac34\pi i}\right)^2}{1+z^{8q+4}}\right)' + {\frac1{8q+4}\sum\limits_{\substack{k=0\dots q-1,\\q+1\dots3q,\\3q+2\dots4q+1}} \lim\limits_{\large{z\to e^{\frac{2k+1}{8q+4}\pi i}}}\dfrac{1+z^2}{1+z^4}\dfrac{z}{z^{4q+2}}}\right)\\ & = \pi i\left(-i(-1)^q\dfrac{\sqrt2}{32q+16} -i(-1)^q\dfrac{\sqrt2}{32q+16} + {\frac1{8q+4}\sum\limits_{\substack{k=0\dots q-1,\\ q+1\dots3q,\\3q+2\dots4q-1}}\dfrac{1+e^{\frac{2k+1}{4q+2}\pi i}}{1+e^{\frac{2k+1}{2q+1}\pi i}}\dfrac{e^{\frac{2k+1}{8q+4}\pi i}}{e^{\frac{2k+1}2\pi i}}}\right)\\ & = \pi i\left(-\dfrac{(-1)^q\sqrt2}{16q+8}i - \dfrac{i}{8q+4}\sum\limits_{\substack{k=0\dots q-1,\\ q+1\dots3q,\\3q+2\dots4q-1}}(-1)^k \dfrac{1+e^{\frac{2k+1}{4q+2}\pi i}}{e^{\frac{2k+1}{8q+4}\pi i}}\dfrac{e^{\frac{2k+1}{4q+2}\pi i}}{1+e^{\frac{2k+1}{2q+1}\pi i}}\right),\\ &I(4q+2) = \dfrac\pi{16q+8}\left((-1)^q\sqrt2 + 2\sum\limits_{\substack{k=0\dots q-1,\\ q+1\dots3q\\3q+2\cdots 4q+1}} (-1)^k \dfrac{\cos\frac{2k+1}{8q+4}\pi}{\cos\frac{2k+1}{4q+2}\pi}\right).\tag5\\ \end{align}

Formula $(5)$ also can be simplified by grouping of the symmetric terms, then \begin{align} &I(4q+2) = \dfrac\pi{16q+8}\left((-1)^q\sqrt2 + 2\sum\limits_{\substack{k=0\dots q-1,\\ q+1\dots2q}}(-1)^k\left(\dfrac{\cos\frac{2k+1}{8q+4}\pi}{\cos\frac{2k+1}{4q+2}\pi} - \dfrac{\cos\frac{2(4q+1-k)+1}{8q+4}\pi}{\cos\frac{2(4q+1-k)+1}{4q+2}\pi}\right)\right)\\ &= \dfrac\pi{16q+8}\left((-1)^q\sqrt2 + 4\sum\limits_{\substack{k=0\dots q-1,\\ q+1\dots2q}} (-1)^k \dfrac{\cos\frac{2k+1}{8q+4}\pi}{\cos\frac{2k+1}{4q+2}\pi}\right)\\ &= \dfrac\pi{16q+8} \left((-1)^q\sqrt2 + 4\sum\limits_{\large{k=0}}^{\large{q-1}}(-1)^k\left( \dfrac{\cos\frac{2k+1}{8q+4}\pi}{\cos\frac{2k+1}{4q+2}\pi} + \dfrac{\cos\frac{2(2q-k)+1}{8q+4}\pi}{\cos\frac{2(2q-k)+1}{4q+2}\pi}\right)\right)\\ &= \dfrac\pi{16q+8}\left((-1)^q\sqrt2 + 4\sum\limits_{\large{k=0}}^{\large{q-1}} (-1)^k \dfrac{\cos\frac{2k+1}{8q+4}\pi - \sin\frac{2k+1}{8q+4}\pi}{\cos\frac{2k+1}{4q+2}\pi}\right)\\ &= \dfrac\pi{16q+8}\left((-1)^q\sqrt2 + 4\sqrt2\sum\limits_{\large{k=0}}^{\large{q-1}} (-1)^k \dfrac{\sin\left(\frac\pi4-\frac{2k+1}{8q+4}\pi\right)}{\sin\left(\frac\pi2-\frac{2k+1}{4q+2}\pi\right)}\right),\\ &\mathbf{\color{blue}{I(4q+2) = \dfrac\pi{(8q+4)\sqrt2} \left((-1)^q + 2\sum\limits_{\large{k=0}}^{\large{q-1}}(-1)^k \sec\frac{q-k}{4q+2}\pi\right).}}\tag6\\ \end{align} The form $(6)$ is closed and allows to get the suitable alternative forms: \begin{align} &\mathbf{\color{green}{I(2) = \dfrac\pi{4\sqrt2}}},\\ &\mathbf{\color{green}{I(6) = \dfrac\pi{12\sqrt6}(4-\sqrt3)}},\\ &\mathbf{\color{green}{I(10) = \dfrac\pi{20\sqrt{10}}\left(10-\sqrt5-2\sqrt2\sqrt{5-\sqrt5}\right)}}\\ \end{align} (see also Wolfram Mathworld).

$\color{blue}{\textbf{Case $p=2q+1$.}}$

Taking in account the polynomials factorization in the forms of \begin{align} &1+z^{4q+2} = \prod\limits_{k=0}^{4q+1} \left(z-\large{e^{\frac{2k+1}{4q+2}\pi i}}\right) = \prod\limits_{k=0}^{2q}\left(z^2-2z\cos\dfrac{2k+1}{4q+2}\pi+1\right)\\[4pt] & = (1+z^2)\prod\limits_{k=0}^{q-1}\left(z^2-2z\cos\dfrac{2k+1}{4q+2}\pi+1\right)\left(z^2-2z\cos\dfrac{2(2q-k)+1}{4q+2}\pi+1\right)\\[4pt] & = (1+z^2)\prod\limits_{k=0}^{q-1}\left(z^2-2z\cos\dfrac{2k+1}{4q+2}\pi+1\right)\left(z^2+2z\cos\dfrac{2k+1}{4q+2}\pi+1\right),\\[4pt] & = (1+z^2)\prod\limits_{k=1}^{q}\left(z^2-2z\cos\dfrac{2k-1}{4q+2}\pi+1\right)\left(z^2+2z\cos\dfrac{2k-1}{4q+2}\pi+1\right),\\[4pt] &1+z^4 = (1+z^2)^2 -2z^2 = \left(z^2-2z\cos\dfrac\pi4+1\right)\left(z^2+2z\cos\dfrac\pi4+1\right),\\ \end{align} the fraction under the integral $(1)$ can be presented in the additive form of $$\begin{align} &f(z,q) = \dfrac{z^{2q+1}}{z^4+1}\dfrac{1+z^2}{1+z^{4p+2}} = \dfrac{2A_0\left(z-\cos\dfrac\pi4\right)+B_0}{z^2-2z\cos\dfrac\pi4+1}+\dfrac{2C_0\left(z+\cos\dfrac\pi4\right)+D_0}{z^2+2z\cos\dfrac\pi4+1}+\sum\limits_{k=1}^q\left(\dfrac{2A_k\left(z-\cos\dfrac{2k-1}{4q+2}\pi\right)+B_k}{z^2-2z\cos\dfrac{2k-1}{4q+2}\pi+1} + \dfrac{2C_k\left(z+\cos\dfrac{2k-1}{4q+2}\pi\right)+D_k}{z^2+2z\cos\dfrac{2k-1}{4q+2}\pi+1}\right), \end{align}\tag7$$ where unknown coefficients $A, B, C, D$ can be defined from the system \begin{cases} 2A_0\left(\large{e^{\frac\pi4i}}-\cos\frac\pi4\right)+B_0 = \lim\limits_{\large{z\to e^{\frac\pi4i}}}(z^2-2z\cos\dfrac\pi4+1)f(z,q)\\[4pt] 2C_0\left(\large{-e^{\frac\pi4i}}+\cos\frac\pi4\right)+D_0 = \lim\limits_{\large{z\to -e^{\frac\pi4i}}}(z^2+2z\cos\dfrac\pi4+1)f(z,q)\\[4pt] 2A_k\left(\large{e^{\frac{2k-1}{4q+2}\pi i}}-\cos\frac{2k-1}{4q+2}\pi\right)+B_k = \lim\limits_{\large{z\to e^{\frac{2k-1}{4q+2}\pi i}}}\left(z^2-2z\cos\dfrac{2k-1}{4q+2}\pi+1\right)f(z,q)\\ 2C_k\left(\large{-e^{\frac{2k-1}{4q+2}\pi i}}+\cos\frac{2k-1}{4q+2}\pi\right)+D_k = \lim\limits_{\large{z\to -e^{\frac{2k-1}{4q+2}\pi i}}}\left(z^2+2z\cos\dfrac{2k-1}{4q+2}\pi+1\right)f(z,q),\tag8\\ \end{cases} which can be simplified, using L'Hospital rule: \begin{align} &2A_0i\sin\frac\pi4+B_0 = \lim\limits_{\large{z\to e^{\frac\pi4i}}}\dfrac{z^{2q+1}+z^{2q+3}}{1+z^{4q+2}}\cdot\lim\limits_{\large{z\to e^{\frac\pi4i}}}\dfrac{z^2-2z\cos\dfrac\pi4+1}{1+z^4}\\[4pt] & = \dfrac{\large{e^{\frac{2q+1}4\pi i}}+e^{\frac{2q+3}4\pi i}}{1+\large{e^{\frac{2q+1}2\pi i}}}\cdot\frac12\lim\limits_{\large{z\to e^{\frac\pi4i}}}\dfrac{z-\cos\dfrac\pi4}{z^3} = \dfrac12\dfrac{\large{e^{\frac{2q+1}4\pi i}}+e^{\frac{2q+3}4\pi i}}{1+\large{e^{\frac{2q+1}2\pi i}}}\cdot\dfrac{i\sin\dfrac\pi4}{\large{e^{\frac34\pi i}}}\\[4pt] & = \dfrac i2\sin\dfrac\pi4\cdot\dfrac{\large{e^{\frac{q-1}2\pi i}}+e^{\frac{q}2\pi i}}{1+\large{e^{\frac{2q+1}2\pi i}}} = \dfrac i2\sin\dfrac\pi4\cdot\dfrac{\large{e^{\frac{2q-1}4\pi i}}}{e^{\frac{2q+1}4\pi i}}\dfrac{\cos\frac\pi4}{\cos\frac{2q+1}4\pi} =\dfrac1{4\cos\frac{2q+1}4\pi}\\[4pt] &=\dfrac12\cos\frac{2q+1}4\pi,\\[4pt] &-2C_0i\sin\frac\pi4+D_0 = \lim\limits_{\large{z\to -e^{\frac\pi4i}}}\dfrac{z^{2q+1}+z^{2q+3}}{1+z^{4q+2}}\cdot\lim\limits_{\large{z\to -e^{\frac\pi4i}}}\dfrac{z^2+2z\cos\dfrac\pi4+1}{1+z^4}\\[4pt] & = \dfrac{\large{-e^{\frac{2q+1}4\pi i}}-e^{\frac{2q+3}4\pi i}}{1+\large{e^{\frac{2q+1}2\pi i}}}\cdot\frac12\lim\limits_{\large{z\to -e^{\frac\pi4i}}}\dfrac{z+\cos\dfrac\pi4}{z^3} = -\dfrac12\dfrac{\large{e^{\frac{2q+1}4\pi i}}+e^{\frac{2q+3}4\pi i}}{1+\large{e^{\frac{2q+1}2\pi i}}}\cdot\dfrac{i\sin\dfrac\pi4}{\large{e^{\frac34\pi i}}}\\[4pt] & = -\dfrac12\cos\frac{2q+1}4\pi,\\[4pt] &2A_ki\sin\frac{2k-1}{4q+2}\pi+B_k = \lim\limits_{\large{z\to e^{\frac{2k-1}{4q+2}\pi i}}}\frac{z^{2q+1}+z^{2q+3}}{1+z^{4}}\cdot\lim\limits_{\large{z\to e^{\frac{2k-1}{4q+2}\pi i}}}\dfrac{z^2-2z\cos\frac{2k-1}{4q+2}\pi+1}{1+z^{4q+2}}\\[4pt] & = \large{e^{\frac{2k-1}2\pi i}}\cdot\frac{1+\large{e^{\frac{2k-1}{2q+1}\pi i}}}{1+\large{e^{\frac{4k-2}{2q+1}\pi i}}}\cdot\normalsize{\frac1{2q+1}}\lim\limits_{\large{z\to e^{\frac{2k-1}{4q+2}\pi i}}}\frac{z-\cos\frac{2k-1}{4q+2}\pi}{z^{4q+1}}\\[4pt] & = \frac1{2q+1}\large{e^{\frac{2k-1}2\pi i}}\frac{1+e^{\frac{2k-1}{2q+1}\pi i}}{1+e^{\frac{4k-2}{2q+1}\pi i}}\cdot\frac{\normalsize{i\sin}\frac{2k-1}{4q+2}\pi}{e^{\frac{2k-1}{4q+2}(4q+1)\pi i}}\\[4pt] & = \frac{(-1)^k}{2q+1}\sin\frac{2k-1}{4q+2}\pi\cdot\frac{1+e^{\frac{2k-1}{2q+1}\pi i}}{1+e^{\frac{4k-2}{2q+1}\pi i}}\cdot\dfrac{e^{\frac{2k-1}{4q+2}\pi i}}{-1}\\[4pt] & = \frac{{(-1)^{k+1}}}{2q+1}\sin\frac{2k-1}{4q+2}\pi\cdot \frac{\cos\frac{2k-1}{4q+2}\pi }{\cos\frac{2k-1}{2q+1}\pi}\cdot\dfrac{\large{e^{\frac{2k-1}{4q+2}\pi i}}e^{\frac{2k-1}{4q+2}\pi i}}{\large{e^{\frac{2k-1}{2q+1}\pi i}}}\\[4pt] & = \frac{(-1)^{k+1}}{4q+2}\tan\frac{2k-1}{2q+1}\pi,\\[4pt] &-2C_ki\sin\frac{2k-1}{4q+2}\pi+D_k = \lim\limits_{\large{z\to -e^{\frac{2k-1}{4q+2}\pi i}}}\frac{z^{2q+1}+z^{2q+3}}{1+z^{4}}\cdot\lim\limits_{\large{z\to -e^{\frac{2k-1}{4q+2}\pi i}}}\dfrac{z^2+2z\cos\frac{2k-1}{4q+2}\pi+1}{1+z^{4q+2}}\\[4pt] & = -\large{e^{\frac{2k-1}2\pi i}}\cdot\frac{1+\large{e^{\frac{2k-1}{2q+1}\pi i}}}{1+\large{e^{\frac{4k-2}{2q+1}\pi i}}}\cdot\normalsize{\frac1{2q+1}}\lim\limits_{\large{z\to -e^{\frac{2k-1}{4q+2}\pi i}}}\frac{z+\cos\frac{2k-1}{4q+2}\pi}{z^{4q+1}}\\[4pt] & = -\frac1{2q+1}\large{e^{\frac{2k-1}2\pi i}}\frac{1+e^{\frac{2k-1}{2q+1}\pi i}}{1+e^{\frac{4k-2}{2q+1}\pi i}}\cdot\frac{\normalsize{i\sin}\frac{2k-1}{4q+2}\pi}{e^{\frac{2k-1}{4q+2}(4q+1)\pi i}}\\[4pt] & = -\frac{(-1)^{k+1}}{4q+2}\tan\frac{2k-1}{2q+1}\pi.\\[4pt] \end{align} Therefore, the coefficients equal to \begin{cases} A_k = C_k = 0,\quad k=0\dots q,\\ B_0 = \dfrac12\cos\frac{2q+1}4\pi\\[4pt] B_k = \frac{(-1)^{k+1}}{4q+2}\tan\frac{2k-1}{2q+1}\pi,\quad k=1\dots q\\[4pt] D_k = -B_k, \quad k=1\dots q,\tag{9} \end{cases} and the decomposition of $(7)$ can be written in the form of \begin{align} &f(z,q) = \dfrac12\cos\frac{2q+1}4\pi\cdot\left(\dfrac1{z^2-2z\cos\dfrac\pi4+1} -\dfrac1{z^2+2z\cos\dfrac\pi4+1}\right)\\[4pt] &+\dfrac1{4q+2}\sum\limits_{k=1}^{q} (-1)^{k+1}\tan\frac{2k-1}{2q+1}\pi\cdot\left(\dfrac{1}{z^2-2z\cos\dfrac{2k-1}{4q+2}\pi+1} - \dfrac1{z^2+2z\cos\dfrac{2k-1}{4q+2}\pi+1}\right)\\[4pt] & = \cos\frac{2q+1}4\pi\cos\dfrac\pi4\cdot\dfrac{2z}{1+z^4}\\[4pt] &+\dfrac1{2q+1}\sum\limits_{k=1}^{q} (-1)^{k+1}\tan\frac{2k-1}{2q+1}\pi\cos\dfrac{2k-1}{4q+2}\pi\cdot\dfrac{2z}{(z^2+1)^2-4z^2\cos^2\dfrac{2k-1}{4q+2}\pi} \\[4pt] & = \cos\frac{2q+1}4\pi\cos\dfrac\pi4\cdot\dfrac{2z}{1+z^4}\\[4pt] &+\dfrac1{2q+1}\sum\limits_{k=1}^{q} (-1)^{k+1}\tan\frac{2k-1}{2q+1}\pi\cos\dfrac{2k-1}{4q+2}\pi\cdot\dfrac{2z}{z^4-2z^2\cos\dfrac{2k-1}{2q+1}\pi+1} \\[4pt] \end{align} $$\begin{align} &f(z,q) = \cos\frac{2q+1}4\pi\cos\dfrac\pi4\cdot\dfrac{2z}{1+z^4}\\[4pt] &+\dfrac1{2q+1}\sum\limits_{k=1}^{q} (-1)^{k+1}\tan\frac{2k-1}{2q+1}\pi\cos\dfrac{2k-1}{4q+2}\pi\cdot\dfrac{2z}{\left(z^2-\cos\dfrac{2k-1}{2q+1}\pi\right)^2+\sin^2\dfrac{2k-1}{2q+1}\pi} \end{align}\tag{10}$$ The sum $(10)$ can be easily integrated, then \begin{align} &I(2q+1) = \left.\cos\frac{2q+1}4\pi\cos\frac\pi4\cdot \mathop{\mathrm{arctan}}z^2\right|_0^\infty\\[4pt] &+\dfrac1{2q+1}\left.\sum\limits_{k=1}^{q} (-1)^{k+1}\dfrac{\cos\frac{2k-1}{4q+2}\pi}{\cos\frac{2k-1}{2q+1}\pi}\cdot\mathop{\mathrm{arctan}}\dfrac{z^2-\cos\dfrac{2k-1}{2q+1}\pi}{\sin\dfrac{2k-1}{2q+1}\pi}\right|_0^\infty\\[4pt] & = \dfrac\pi2\cos\frac{2q+1}4\pi\cos\frac\pi4 +\dfrac1{2q+1}\sum\limits_{k=1}^{q} (-1)^{k+1}\dfrac{\cos\frac{2k-1}{4q+2}\pi}{\cos\frac{2k-1}{2q+1}\pi}\left(\dfrac\pi2+\dfrac\pi2-\frac{2k-1}{2q+1}\pi\right) \end{align} $$\mathbf{\color{blue}{I(p) = \dfrac\pi4\left(2\cos\frac{p}4\pi\cos\frac\pi4 +\dfrac8{p^2}\sum\limits_{k=1}^{\frac{p-1}2} (-1)^{k+1}\left(\frac{p+1}2-k\right)\dfrac{\sin\frac{p+1-2k}{2p}\pi}{\cos\frac{2k-1}{p}\pi}\right).}}\tag{11}$$

The closed form $(11)$ is valid for the odd positive numbers and allows to get the suitable alternative forms: \begin{align} &\mathbf{\color{green}{I(1) = \dfrac\pi4}},\\[4pt] &\mathbf{\color{green}{I(3) = \dfrac\pi4\left(\dfrac89\tan\dfrac\pi3-1\right) = \dfrac\pi4\left(\dfrac8{3\sqrt3}-1\right)}},\\[4pt] &\mathbf{\color{green}{I(5) = \dfrac\pi4\left(\dfrac8{25} \left(2\dfrac{\sin\frac{2}{5}\pi}{\cos\frac{1}{5}\pi}+\dfrac{\sin\frac{1}{5}\pi}{\cos\frac{2}{5}\pi}\right)-1\right)}}\\[4pt] &\mathbf{\color{green}{ = \dfrac\pi{100}\left(32\sin\frac{\pi}{5}+16\sin\frac{2\pi}{5}-25\right) = \dfrac\pi{20}\left(4\sqrt{2+\frac2{\sqrt5}}-5\right).}}\\[4pt] \end{align} If the solution can not be presented through the radicals, expression $(11)$ can be simplified in this case too. For example, \begin{align} &\dfrac4\pi I(7) = \dfrac8{49} \left( 3\dfrac{\sin\frac{3}{7}\pi}{\cos\frac{1}{7}\pi} -2\dfrac{\sin\frac{2}{7}\pi}{\cos\frac{3}{7}\pi} +\dfrac{\sin\frac{1}{7}\pi}{\cos\frac{5}{7}\pi}\right)+1\\[4pt] & = \dfrac8{49} \left( 3\dfrac{\sin\frac{4}{7}\pi}{\cos\frac{1}{7}\pi} +2\dfrac{\sin\frac{2}{7}\pi}{\cos\frac{4}{7}\pi} -\dfrac{\sin\frac{1}{7}\pi}{\cos\frac{2}{7}\pi}\right)+1\\[4pt] &= \dfrac8{49} \dfrac{ 3\sin\frac{4}{7}\pi\cos\frac{2}{7}\pi\cos\frac{4}{7}\pi +2\sin\frac{2}{7}\pi\cos\frac{1}{7}\pi\cos\frac{2}{7}\pi -\sin\frac{1}{7}\pi\cos\frac{1}{7}\pi\cos\frac{4}{7}\pi}{\cos\frac{1}{7}\pi\cos\frac{2}{7}\pi\cos\frac{4}{7}\pi}\cdot \dfrac{\sin\frac{1}{7}\pi}{\sin\frac{1}{7}\pi}+1\\[4pt] &= \dfrac4{49}\left(3\sin\frac{8}{7}\pi\cos\frac{2}{7}\pi+2\cos\frac{1}{7}\pi\sin\frac{4}{7}\pi-\cos\frac{4}{7}\pi\sin\frac{2}{7}\pi\right)\cdot\dfrac{8\sin\frac{1}{7}\pi}{\sin\frac{8}{7}\pi}+1\\[4pt] &= \dfrac{32}{49}\left(3\sin\frac{1}{7}\pi\cos\frac{2}{7}\pi -2\sin\frac{3}{7}\pi\cos\frac{1}{7}\pi -\sin\frac{2}{7}\pi\cos\frac{3}{7}\pi\right)+1\\[4pt] &= \dfrac\pi4\left(\dfrac{16}{49}\left(-2\sin\frac{1}{7}\pi - 3\sin\frac{2}{7}\pi + \sin\frac{3}{7}\pi\right)+1\right)\\[4pt] &\mathbf{\color{green}{I(7) = \dfrac\pi{196}\left(49 - 32\sin\frac{1}{7}\pi - 48\sin\frac{2}{7}\pi + 16\sin\frac{4}{7}\pi\right)}}.\\[4pt] \end{align} \begin{align} &\dfrac4\pi I(9) = \dfrac8{81} \left( 4\dfrac{\sin\frac{4}{9}\pi}{\cos\frac{1}{9}\pi} -3\dfrac{\sin\frac{3}{9}\pi}{\cos\frac{3}{9}\pi} +2\dfrac{\sin\frac{2}{9}\pi}{\cos\frac{5}{9}\pi} -\dfrac{\sin\frac{1}{9}\pi}{\cos\frac{7}{9}\pi}\right)+1\\[4pt] & = \dfrac8{81} \left( 4\dfrac{\sin\frac{4}{9}\pi}{\cos\frac{1}{9}\pi} +\dfrac{\sin\frac{1}{9}\pi}{\cos\frac{2}{9}\pi} -2\dfrac{\sin\frac{2}{9}\pi}{\cos\frac{4}{9}\pi} -6\sin\frac{1}{3}\pi\right)+1\\[4pt] &= \dfrac8{81} \Bigg(\dfrac{ 4\sin\frac{4}{9}\pi\cos\frac{2}{9}\pi\cos\frac{4}{9}\pi +\sin\frac{1}{9}\pi\cos\frac{1}{9}\pi\cos\frac{4}{9}\pi -2\sin\frac{2}{9}\pi\cos\frac{1}{9}\pi\cos\frac{2}{9}\pi }{\cos\frac{1}{9}\pi\cos\frac{2}{9}\pi\cos\frac{4}{9}\pi}\cdot \dfrac{\sin\frac{1}{9}\pi}{\sin\frac{1}{9}\pi}\\[4pt] &-6\sin\frac{1}{3}\pi\Bigg)+1\\[4pt] &= \dfrac{8}{81} \left(\left( 2\sin\frac{8}{9}\pi\cos\frac{2}{9}\pi +\frac12\sin\frac{2}{9}\pi\cos\frac{4}{9}\pi -4\sin\frac{4}{9}\pi\cos\frac{1}{9}\pi \right)\cdot\dfrac{8\sin\frac{1}{9}\pi}{\sin\frac{8}{9}\pi} -3\sin\frac{1}{3}\pi\right)+1\\[4pt] &= \dfrac{16}{81} \left( 8\sin\frac{1}{9}\pi\cos\frac{2}{9}\pi +2\sin\frac{2}{9}\pi\cos\frac{4}{9}\pi -4\sin\frac{4}{9}\pi\cos\frac{1}{9}\pi -3\sin\frac{1}{3}\pi\right)+1\\[4pt] &= \dfrac{\pi}{324} \left(81 -64\sin\frac{1}{9}\pi -16\sin\frac{2}{9}\pi -32\sin\frac{4}{9}\pi\right)\\[4pt] &\mathbf{\color{green}{I(9) = \dfrac{\pi}{324} \left(81 -64\sin\frac{\pi}{9} -16\sin\frac{2\pi}{9} -32\sin\frac{4\pi}{9}\right)}}.\\[4pt] \end{align} Also, for $p=17$ due to Wolfram Alpha can be found the similar closed form: \begin{align} &\dfrac4\pi I(17) = \dfrac8{289} \left( 8\dfrac{\sin\frac{8\pi}{17}}{\cos\frac{\pi}{17}} -7\dfrac{\sin\frac{7\pi}{17}}{\cos\frac{3\pi}{17}} +6\dfrac{\sin\frac{6\pi}{17}}{\cos\frac{5\pi}{17}} -5\dfrac{\sin\frac{5\pi}{17}}{\cos\frac{7\pi}{17}} +4\dfrac{\sin\frac{4\pi}{17}}{\cos\frac{9\pi}{17}} -3\dfrac{\sin\frac{3\pi}{17}}{\cos\frac{11\pi}{17}} +2\dfrac{\sin\frac{2\pi}{17}}{\cos\frac{13\pi}{17}} -\dfrac{\sin\frac{\pi}{17}}{\cos\frac{15\pi}{17}}\right)+1\\[4pt] &= \dfrac8{289} \left( 8\dfrac{\sin\frac{8\pi}{17}}{\cos\frac{\pi}{17}} +\dfrac{\sin\frac{\pi}{17}}{\cos\frac{2\pi}{17}} -2\dfrac{\sin\frac{2\pi}{17}}{\cos\frac{4\pi}{17}} -4\dfrac{\sin\frac{4\pi}{17}}{\cos\frac{8\pi}{17}}\right) +\dfrac8{289}\left( 7\dfrac{\sin\frac{24\pi}{17}}{\cos\frac{3\pi}{17}} +3\dfrac{\sin\frac{3\pi}{17}}{\cos\frac{6\pi}{17}} -6\dfrac{\sin\frac{6\pi}{17}}{\cos\frac{12\pi}{17}} +5\dfrac{\sin\frac{12\pi}{17}}{\cos\frac{24\pi}{17}} \right)+1\\[4pt] &= 1 + \frac{4\sin\frac{\pi}{17}}{289\sin\frac{\pi}{17}} \times \frac{ 8\sin\frac{16\pi}{17}\cos\frac{2\pi}{17}\cos\frac{4\pi}{17} +\sin\frac{2\pi}{17}\cos\frac{4\pi}{17}\cos\frac{8\pi}{17} -2\sin\frac{4\pi}{17}\cos\frac{\pi}{17}\cos\frac{8\pi}{17} -4\sin\frac{8\pi}{17}\cos\frac{\pi}{17}\cos\frac{2\pi}{17}}{\cos\frac{\pi}{17}\cos\frac{2\pi}{17}\cos\frac{4\pi}{17}\cos\frac{8\pi}{17}} + \frac{4\sin\frac{3\pi}{17}}{289\sin\frac{3\pi}{17}}\times \frac{ {7\sin\frac{48\pi}{17}\cos\frac{6\pi}{17}\cos\frac{12\pi}{17}} {+3\sin\frac{6\pi}{17}\cos\frac{12\pi}{17}\cos\frac{24\pi}{17}} {-6\sin\frac{12\pi}{17}\cos\frac{24\pi}{17}\cos\frac{3\pi}{17}} {+5\sin\frac{24\pi}{17}\cos\frac{3\pi}{17}\cos\frac{6\pi}{17}}}{\cos\frac{3\pi}{17}\cos\frac{6\pi}{17}\cos\frac{12\pi}{17}\cos\frac{24\pi}{17}}\\[4pt] \end{align} \begin{align} &= 1 + \frac{64\sin\frac{\pi}{17}}{289\sin\frac{16\pi}{17}} \Big( 8\sin\frac{\pi}{17}\cos\frac{2\pi}{17}\cos\frac{4\pi}{17} +\sin\frac{2\pi}{17}\cos\frac{4\pi}{17}\cos\frac{8\pi}{17} -2\sin\frac{4\pi}{17}\cos\frac{\pi}{17}\cos\frac{8\pi}{17}\\[4pt] &-4\sin\frac{8\pi}{17}\cos\frac{\pi}{17}\cos\frac{2\pi}{17}\Big) + \frac{64\sin\frac{3\pi}{17}}{289\sin\frac{48\pi}{17}} \Big({7\sin\frac{48\pi}{17}\cos\frac{6\pi}{17}\cos\frac{12\pi}{17}}\\[4pt] &{+3\sin\frac{6\pi}{17}\cos\frac{12\pi}{17}\cos\frac{24\pi}{17}} {-6\sin\frac{12\pi}{17}\cos\frac{24\pi}{17}\cos\frac{3\pi}{17}} {+5\sin\frac{24\pi}{17}\cos\frac{3\pi}{17}\cos\frac{6\pi}{17}}\Big)\\[4pt] \end{align} \begin{align} &= 1 + \frac{64}{289}\Bigg( \left(8\sin\frac{\pi}{17}\cos\frac{4\pi}{17} -4\sin\frac{8\pi}{17}\cos\frac{\pi}{17}\right)\cos\frac{2\pi}{17}\\[4pt] &+\left(6\sin\frac{5\pi}{17}\cos\frac{7\pi}{17} -5\sin\frac{7\pi}{17}\cos\frac{6\pi}{17}\right)\cos\frac{3\pi}{17}\\[4pt] &+\left(\sin\frac{2\pi}{17}\cos\frac{4\pi}{17} -2\sin\frac{4\pi}{17}\cos\frac{\pi}{17}\right)\cos\frac{8\pi}{17}\\[4pt] &+\left(3\sin\frac{6\pi}{17}\cos\frac{7\pi}{17} -7\sin\frac{3\pi}{17}\cos\frac{6\pi}{17}\right)\cos\frac{5\pi}{17} \Bigg)\\[4pt] &= 1 + \frac{16}{289}\Bigg( -2\sin\frac{\pi}{17}-3\sin\frac{2\pi}{17}+10\sin\frac{3\pi}{17}-9\sin\frac{4\pi}{17}-16\sin\frac{5\pi}{17}-2\sin\frac{6\pi}{17}+4\sin\frac{7\pi}{17}+10\sin\frac{8\pi}{17}+2\sin\frac{9\pi}{17}-6\sin\frac{10\pi}{17}-6\sin\frac{11\pi}{17}-2\sin\frac{13\pi}{17}-6\sin\frac{14\pi}{17}+6\sin\frac{15\pi}{17}-5\sin\frac{16\pi}{17}+3\sin\frac{18\pi}{17}\Bigg)\\[4pt] &= 1 + \frac{16}{289}\Bigg( -10\sin\frac{\pi}{17}+3\sin\frac{2\pi}{17}+4\sin\frac{3\pi}{17}-11\sin\frac{4\pi}{17}-16\sin\frac{5\pi}{17}-8\sin\frac{6\pi}{17}-2\sin\frac{7\pi}{17}+12\sin\frac{8\pi}{17}\Bigg)\\[4pt] \end{align}

\begin{align} &\mathbf{\color{green}{I(17) = \dfrac\pi4+\dfrac{4\pi}{289}\left(-10\sin\frac{\pi}{17}+3\sin\frac{2\pi}{17}+4\sin\frac{3\pi}{17}-11\sin\frac{4\pi}{17}-16\sin\frac{5\pi}{17}-8\sin\frac{6\pi}{17}-2\sin\frac{7\pi}{17}+12\sin\frac{8\pi}{17}\right).}}\\ \end{align} Obtained form can be presented via radicals: \begin{align} &\mathbf{\color{green}{I(17) = \dfrac\pi4+\dfrac{\pi}{289}S_{17}}},\\ \end{align} \begin{align} &\mathbf{\color{green}{S_{17}=6\sqrt{2{17-\sqrt{17}+\sqrt{2{17-\sqrt{17}}}+\sqrt{2{34+6\sqrt{17}+\sqrt{2{17-\sqrt{17}}}-\sqrt{34{17-\sqrt{17}}}+8\sqrt{2{17+\sqrt{17}}}}}}}-8\sqrt{2{17+\sqrt{17}-\sqrt{2{17+\sqrt{17}}}+\sqrt{2{34-6\sqrt{17}+8\sqrt{2{17-\sqrt{17}}}-\sqrt{2{17+\sqrt{17}}}-\sqrt{34{17+\sqrt{17}}}}}}}-4\sqrt{2{17+\sqrt{17}+\sqrt{2{17+\sqrt{17}}}-\sqrt{2{34-6\sqrt{17}-8\sqrt{2{17-\sqrt{17}}}+\sqrt{2{17+\sqrt{17}}}+\sqrt{34{17+\sqrt{17}}}}}}}-\sqrt{2{17+\sqrt{17}+\sqrt{2{17+\sqrt{17}}}+\sqrt{2{34-6\sqrt{17}-8\sqrt{2{17-\sqrt{17}}}+\sqrt{2{17+\sqrt{17}}}+\sqrt{34{17+\sqrt{17}}}}}}}+3\sqrt{8-\sqrt{2{15+\sqrt{17}+\sqrt{2{17-\sqrt{17}}}-\sqrt{2{34+6\sqrt{17}-\sqrt{2{17-\sqrt{17}}}+\sqrt{34{17-\sqrt{17}}}-8\sqrt{2{17+\sqrt{17}}}}}}}}-11\sqrt{8-\sqrt{2{15+\sqrt{17}-\sqrt{2{17-\sqrt{17}}}-\sqrt{2{34+6\sqrt{17}+\sqrt{2{17-\sqrt{17}}}-\sqrt{34{17-\sqrt{17}}}+8\sqrt{2{17+\sqrt{17}}}}}}}}-10\sqrt{8-\sqrt{2{15+\sqrt{17}-\sqrt{2{17-\sqrt{17}}}+\sqrt{2{34+6\sqrt{17}+\sqrt{2{17-\sqrt{17}}}-\sqrt{34{17-\sqrt{17}}}+8\sqrt{2{17+\sqrt{17}}}}}}}}+4\sqrt{8-\sqrt{2{15-\sqrt{17}-\sqrt{2{17+\sqrt{17}}}+\sqrt{2{34-6\sqrt{17}-8\sqrt{2{17-\sqrt{17}}}+\sqrt{2{17+\sqrt{17}}}+\sqrt{34{17+\sqrt{17}}}}}}}} }}.\\ \end{align}

$$\color{blue}{\textbf{Conclusions.}}$$

The issue integral can be presented in closed form for the non-negative integer values of the parameter $p,$ using the formulas

$$I(p) = \begin{cases} \dfrac\pi{2\sqrt2},\text{ if } p=0\\[4pt] \dfrac\pi{2q\sqrt2} \left((-1)^q\cdot q + \dfrac12\sum\limits_{\large{k=0}}^{\large{q-1}}(-1)^k \sec\frac{2q+2k+1}{8q}\pi\right),\text{ if } p = 4q,\quad q\in\mathbb N\\[4pt] \dfrac\pi{(8q+4)\sqrt2} \left((-1)^q + 2\sum\limits_{\large{k=0}}^{\large{q-1}}(-1)^k \sec\frac{q-k}{4q+2}\pi\right),\text{ if } p=4q+2,\quad q\in\mathbb N\\[4pt] \dfrac\pi4\left(2\cos\frac{p}4\pi\cos\frac\pi4 +\dfrac8{p^2}\sum\limits_{k=1}^{\frac{p-1}2} (-1)^{k+1}\left(\frac{p+1}2-k\right)\dfrac{\sin\frac{p+1-2k}{2p}\pi}{\cos\frac{2k-1}{p}\pi}\right),\text{ if $p$ is odd}. \end{cases}\tag{12} $$

Besides, there were obtained the simplified closed forms in the next partial cases:

$$\begin{cases} I(1) = \dfrac\pi4\\[4pt] I(2) = \dfrac\pi{4\sqrt2}\\[4pt] I(3) = \dfrac\pi4\left(\dfrac89\tan\dfrac\pi3-1\right) = \dfrac\pi4\left(\dfrac8{3\sqrt3}-1\right)\\[4pt] I(4) = \dfrac\pi{2\sqrt2}\left(\sqrt2\cos\frac\pi8-1\,\right) = \dfrac\pi4\left(\sqrt{2+\sqrt2}-\sqrt2\,\right)\\[4pt] I(5) = \dfrac\pi4\left(\dfrac{16}{25}\left(2\sin\frac{1}{5}\pi+\sin\frac{2}{5}\pi\right)-1\right) = \dfrac\pi4\left(\dfrac45\sqrt{2+\frac2{\sqrt5}}-1\right)\\[4pt] I(6) = \dfrac\pi{12\sqrt6}(4-\sqrt3)\\[4pt] I(7) = \dfrac\pi{196}\left(49 - 32\sin\frac{1}{7}\pi - 48\sin\frac{2}{7}\pi + 16\sin\frac{4}{7}\pi\right)\\[4pt] I(8) = \dfrac\pi{4\sqrt2}\left(2-\sqrt{2+\sqrt{2-\sqrt2}}\,\right)\\[4pt] I(9) = \dfrac{\pi}{324} \left(81 -64\sin\frac{\pi}{9} -16\sin\frac{2\pi}{9} -32\sin\frac{4\pi}{9}\right)\\[4pt] I(10) = \dfrac\pi{20\sqrt{10}}\left(10-\sqrt5-2\sqrt2\sqrt{5-\sqrt5}\right)\\[4pt] I(12) = \dfrac\pi{6\sqrt2}\left(\sqrt{7+\sqrt{\frac{49}2-4\sqrt3}}-3\right)\\[4pt] I(16) = \dfrac\pi{8\sqrt2}\Bigg(4 + \frac1{\sqrt{2-\sqrt{2-\sqrt{2+\sqrt2}}}} - \frac1{\sqrt{2-\sqrt{2-\sqrt{2-\sqrt2}}}} + \frac1{\sqrt{2-\sqrt{2+\sqrt{2-\sqrt2}}}} - \frac1{\sqrt{2-\sqrt{2+\sqrt{2+\sqrt2}}}}\Bigg)\\[4pt] I(17) = \dfrac\pi4+\dfrac{4\pi}{289}\left(-10\sin\frac{\pi}{17}+3\sin\frac{2\pi}{17}+4\sin\frac{3\pi}{17}-11\sin\frac{4\pi}{17}-16\sin\frac{5\pi}{17}-8\sin\frac{6\pi}{17}-2\sin\frac{7\pi}{17}+12\sin\frac{8\pi}{17}\right),\tag{13}\\ \end{cases}$$

wherein all solutions with $p>5$ are not in OP. For $p=17$ obtained closed form in radicals.

$$\color{brown}{\textbf{Parameter $p$ is irreducible fraction.}}$$

The simplest way is using formulas of OP for the transformation of the integer solutions.

Another way is the substitutions $$p=\dfrac mn,\quad z=t^n,$$ then $$I(p)=n\int\limits_0^\infty\dfrac{1+t^{2n}}{1+t^{4n}}\dfrac{t^{m+n-1}}{1+t^{2m}}\,\mathrm dt,\tag{14}$$ and to try methods of this answer.

On the other hand, the integral $(14)$ has two parameters, and this makes it very hard.