Integrate $\int\frac{\cos^2x}{1+\tan x}dx$ Integrate $$I=\int\frac{\cos^2x}{1+\tan x}dx$$

$$I=\int\frac{\cos^3xdx}{\cos x+\sin x}=\int\frac{\cos^3x(\cos x-\sin x)dx}{\cos^2x-\sin^2x}=\int\frac{\cos^4xdx}{1-2\sin^2x}-\int\frac{\cos^3x\sin xdx}{2\cos^2x-1}$$
Let $t=\sin x,u=\cos x,dt=\cos xdx,du=-\sin xdx$
$$I=\underbrace{\int\frac{-u^4du}{(2u^2-1)\sqrt{1-u^2}}}_{I_1}+\underbrace{\int\frac{u^3du}{2u^2-1}}_{I_2}$$
I have found(using long division): $$I_2=\frac{u^2}2+\frac18\ln|2u^2-1|+c=\frac12\cos^2x+\frac18\ln|\cos2x|$$
I have converted $I_1$ into this:
$$I_1=\frac12\left(\int(-2)\sqrt{1-u^2}du+\int\frac{du}{\sqrt{1-u^2}}\right)+\frac14\underbrace{\int\frac{du}{(2u^2-1)\sqrt{1-u^2}}}_{I_3}$$
Now I have took $v=1/u$ in $I_3$ so that $du=-(1/v^2)dv$:
$$I_3=\int\frac{vdv}{(v^2-2)\sqrt{v^2-1}}$$
Now I took $w^2=v^2-1$ or $wdw=vdv$ to get:
$$I_3=\int\frac{dw}{w^2-1}=\frac12\ln\left|\frac{w-1}{w+1}\right|$$
I have not yet formulated the entire thing;



*

*Is this correct?

*This is very long, do you have any "shorter" method?


The integral is equivalent to $ \displaystyle{\int\frac{\cos^3 x}{\sin x + \cos x}\,\mathrm{d}x}. $ Now, consider
$$ \mathcal{I}_{1} = \int\frac{\cos^3 x}{\sin x + \cos x}\,\mathrm{d}x \qquad\text{and}\qquad\mathcal{I}_{2}=\int\frac{\sin^3 x}{\sin x + \cos x}\,\mathrm{d}x. $$
Observe that
$$
\begin{aligned}
\mathcal{I}_{1}+\mathcal{I}_{2} &= \int\frac{\cos^3 x + \sin^3 x}{\sin x + \cos x}\,\mathrm{d}x\\
&=\int\frac{(\cos x + \sin x)(\cos^2 x - \sin x\cos x + \sin^2 x)}{\cos x + \sin x}\,\mathrm{d}x\\
&=x - \frac{\sin^2 x}{2}+C_{1},
\end{aligned}
$$
and
$$
\begin{aligned}
\mathcal{I}_{1}-\mathcal{I}_{2}&=\int\frac{(\cos x - \sin x)(1 + \sin x \cos x)}{\cos x + \sin x}\,\mathrm{d}x\\
&=\int\frac{(\cos x - \sin x)(\cos x + \sin x)(1 + \sin x\cos x)}{(\cos x + \sin x)^2}\,\mathrm{d}x\\
&=\int\frac{1 +\frac{\sin 2x}{2}}{1 + \sin 2x}\cos 2x\,\mathrm{d}x\\
&=\underbrace{\frac{1}{4}\int\left(1 + \frac{1}{1+\sin 2x}\right)2\cos 2x\,\mathrm{d}x}_{t = \sin 2x\implies\mathrm{d}t=2\cos 2x\,\mathrm{d}x}\\
&=\frac{1}{4}\int1+\frac{1}{1+t}\,\mathrm{d}t\\
&=\frac{t}{4}+\frac{1}{4}\log|t+1|+C_{2}\\
&=\frac{\sin2x}{4}+\frac{1}{4}\log(1+\sin2x)+C_{2}
\end{aligned}
$$.
Thus,
$$
\begin{aligned}
(\mathcal{I}_{1}+\mathcal{I}_{2})+(\mathcal{I}_{1}-\mathcal{I}_{2})&=2\mathcal{I}_{1}\\
&=x+\frac{\sin2x}{4}+\frac{1}{4}\log(1+\sin2x)-\frac{\sin^2 x}{2} + C_{3},
\end{aligned}
$$
which yields
$$
\begin{aligned}
\mathcal{I}_{1}&=\int\frac{\cos^3x}{\sin x + \cos x}\,\mathrm{d}x\\
&=\int\frac{\cos^2 x}{1+\tan x}\,\mathrm{d}x\\
&=\frac{x}{2}+\frac{\sin 2x}{8}+\frac{1}{8}\log(1+\sin2x)-\frac{\sin^2 x}{4} + C,
\end{aligned}
$$
or, in an equivalent manner,
$$
\int\frac{\cos^2 x}{1+\tan x}\,\mathrm{d}x=\frac{x}{2}+\frac{\sin x\cos x}{4}+\frac{1}{4}\log\left|\cos\!\left(x-\frac{\pi}{4}\right)\right|-\frac{\sin^2 x}{4}+\mathcal{C}.
$$
